Comp
Comp
Program:
Input:
o Array: {1, 2, 3, 4, 5}
o Target: 4
Output:
o Element found: true
Input:
o Array: {3, 8, 2, 1, 8}
o Target: 8
Output:
o Position of first occurrence: 1
Input:
o Array: {5, 2, 5, 3, 5}
o Target: 5
Output:
o Occurrences of target: 3
Input:
o Array: {9, 7, 5, 3, 1}
o Target: 5
Output:
o Array contains target: true
Input:
o Array: {5, 3, 8, 2, 9}
Output:
o Smallest element: 2
Input:
o Array: {3, 5, 9, 7, 6}
Output:
o Largest element: 9
Input:
o Array: {1, 2, 3, 4, 5}
Output:
o Sum of elements: 15
Input: arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, target = 11
Output: Element found at index: 4
Variable Description:
Variable Type Description
arr int[] Array of sorted integers
target int Element to search for
Result index of the found
result int
element
Left boundary of the search
left int
range
Right boundary of the search
right int
range
Middle index of the current
mid int
search range
static int binarySearch(int[] arr, int target, int left, int right) {
if (left > right) {
return -1;
}
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
return binarySearch(arr, target, mid + 1, right);
} else {
return binarySearch(arr, target, left, mid - 1);
}
}
}
Input: arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, target = 19
Output: Element found at index: 7
Variable Description:
Variable Type Description
arr int[] Array of sorted integers
target int Element to search for
Result index of the found
result int
element
Left boundary of the search
left int
range
Right boundary of the search
right int
range
Middle index of the current
mid int
search range
Input: arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29}, target = 23
Output:Element found at index: 8
Variable Description:
Variable Type Description
arr int[] Array of sorted integers
target int Element to search for
Result index of the found
result int
element
Left boundary of the search
left int
range
Right boundary of the search
right int
range
Middle index of the current
mid int
search range
Array to count the number of
steps int[]
steps taken
Variable Description:
Variable Type Description
arr String[] Array of sorted strings
target String String to search for
Result index of the found
result int
string
Left boundary of the search
left int
range
Right boundary of the search
right int
range
Middle index of the current
mid int
search range
5. Binary Search for Finding First Occurrence
Program:
Variable Description:
Variable Type Description
arr int[] Array of sorted integers
target int Element to search for
Result index of the first
result int
occurrence
Left boundary of the search
left int
range
Right boundary of the search
right int
range
Middle index of the current
mid int
search range
6. Binary Search for Finding Last Occurrence
Program:
Variable Description:
Variable Type Description
arr int[] Array of sorted integers
target int Element to search for
Result index of the last
result int
occurrence
Left boundary of the search
left int
range
Right boundary of the search
right int
range
Middle index of the current
mid int
search range
Variable Description:
Variable Type Description
arr int[] Array of integers to be sorted
i int Loop index for the outer loop
j int Loop index for the inner loop
Index of the minimum
minIndex int
element found
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
arr int[] Array of integers to be sorted
i int Loop index for the outer loop
j int Loop index for the inner loop
Index of the maximum
maxIndex int
element found
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
arr int[] Array of integers to be sorted
i int Loop index for the outer loop
j int Loop index for the inner loop
Index of the minimum
minIndex int
element found
Temporary variable for
temp int
swapping
import java.util.Random;
Variable Description:
Variable Type Description
Array of random integers to
arr int[]
be sorted
i int Loop index for the outer loop
j int Loop index for the inner loop
Index of the minimum
minIndex int
element found
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
arr int[] Already sorted array
i int Loop index for the outer loop
j int Loop index for the inner loop
Index of the minimum
minIndex int
element found
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
Array of floating-point
arr float[]
numbers to be sorted
i int Loop index for the outer loop
j int Loop index for the inner loop
Index of the minimum
minIndex int
element found
Temporary variable for
temp float
swapping
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class SelectionSortCustomClass {
public static void main(String[] args) {
Student[] students = {
new Student("Alice", 22),
new Student("Bob", 20),
new Student("Charlie", 23)
};
selectionSort(students);
System.out.println("Sorted students by age: ");
for (Student student : students) {
System.out.println(student.name + " - " + student.age);}}
static void selectionSort(Student[] students) {
for (int i = 0; i < students.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < students.length; j++) {
if (students[j].age < students[minIndex].age) {
minIndex = j;}}
// Swap the found minimum element with the first element
Student temp = students[minIndex];
students[minIndex] = students[i];
students[i] = temp;}}}
Variable Description:
Variable Type Description
Array of Student objects
students Student[]
to be sorted
Loop index for the outer
i int
loop
Loop index for the inner
j int
loop
Index of the minimum age
minIndex int
found
Temporary variable for
temp Student
swapping
Variable Description:
Variable Type Description
arr int[] Array of integers to be sorted
n int Length of the array
i int Loop index for the outer loop
j int Loop index for the inner loop
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
Array of integers to be
arr int[]
sorted
n int Length of the array
Loop index for the outer
i int
loop
Loop index for the inner
j int
loop
Flag to check if a swap
swapped boolean
occurred
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
arr int[] Already sorted array
n int Length of the array
i int Loop index for the outer loop
j int Loop index for the inner loop
Temporary variable for
temp int
swapping
Variable Description:
Variable Type Description
Array of floating-point
arr float[]
numbers to be sorted
n int Length of the array
i int Loop index for the outer loop
j int Loop index for the inner loop
Temporary variable for
temp float
swapping
class Student {
String name;
int age;
Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class BubbleSortCustomClass {
public static void main(String[] args) {
Student[] students = {
new Student("Alice", 22),
new Student("Bob", 20),
new Student("Charlie", 23)
};
bubbleSort(students);
System.out.println("Sorted students by age: ");
for (Student student : students) {
System.out.println(student.name + " - " + student.age);}}
static void bubbleSort(Student[] students) {
int n = students.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (students[j].age > students[j + 1].age) {
// Swap students[j] and students[j+1]
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}}}}
Variable Description:
Variable Type Description
Array of Student objects to
students Student[]
be sorted
n int Length of the array
Loop index for the outer
i int
loop
Loop index for the inner
j int
loop
Temporary variable for
temp Student
swapping
Variable Description:
Variable Type Description
arr String[] Array of strings to be sorted
n int Length of the array
Loop index for the outer
i int
loop
j int Loop index for the inner loop
Temporary variable for
temp String
swapping
Variable Description:
Variable Type Description
arr String[] Array of strings to be sorted
n int Length of the array
Loop index for the outer
i int
loop
j int Loop index for the inner loop
Temporary variable for
temp String
swapping
STRING
1. Reverse a String
Program:
import java.util.Scanner;
Input: Hello
Output: Reversed string: olleH
Variable Description
Variable Type Description
The string entered by the
input String
user.
The reversed version of the
reversed String
input string.
import java.util.Scanner;
Variable Description
Variable Type Description
The string entered by the
input String
user.
The count of vowels in the
count int
input string.
import java.util.Scanner;
if (input.equalsIgnoreCase(reversed)) {
System.out.println("The string is a palindrome.");
} else {
System.out.println("The string is not a palindrome.");
}
scanner.close();
}
}
Input: Madam
Output: The string is a palindrome.
Variable Description
Variable Type Description
The string entered by the
input String
user.
The reversed version of the
reversed String
input string.
import java.util.Scanner;
Variable Description
Variable Type Description
input String The sentence entered by the
Variable Type Description
user.
An array of words split from
words String[]
the input string.
import java.util.Scanner;
Input: hello
Output: Uppercase string: HELLO
Variable Description
Variable Type Description
The string entered by the
input String
user.
The input string converted
uppercase String
to uppercase.
import java.util.Scanner;
Variable Description
Variable Type Description
The string entered by the
input String
user.
import java.util.Scanner;