0% found this document useful (0 votes)
7 views

Comp

Uploaded by

hanumanrana24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Comp

Uploaded by

hanumanrana24
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Linear Search Programs

1. Search for a Number in an Array

Program:

public class LinearSearch1 {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int target = 4;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
found = true;
break;
}
}
System.out.println("Element found: " + found);
}
}

 Input:
o Array: {1, 2, 3, 4, 5}
o Target: 4
 Output:
o Element found: true

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Target number to
target int
search for
Indicates if the
found boolean
target is found
Loop index for
i int
traversing array
2. Find Position of First Occurrence
 Program:

public class LinearSearch2 {


public static void main(String[] args) {
int[] arr = {3, 8, 2, 1, 8};
int target = 8;
int position = -1;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
position = i;
break;
}
}
System.out.println("Position of first occurrence: " + position);
}
}

 Input:
o Array: {3, 8, 2, 1, 8}
o Target: 8

 Output:
o Position of first occurrence: 1

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Target number to
target int
search for
Stores position of first
position int
occurrence
Loop index for
i int
traversing array
3. Count Occurrences of a Number
 Program:

public class LinearSearch3 {


public static void main(String[] args) {
int[] arr = {5, 2, 5, 3, 5};
int target = 5;
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
count++;
}
}
System.out.println("Occurrences of target: " + count);
}
}

 Input:
o Array: {5, 2, 5, 3, 5}
o Target: 5

 Output:
o Occurrences of target: 3

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Target number to
target int
search for
Stores occurrences of
count int
target
Loop index for
i int
traversing array

4. Check if Array Contains a Number


 Program:
public class LinearSearch4 {
public static void main(String[] args) {
int[] arr = {9, 7, 5, 3, 1};
int target = 5;
boolean contains = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
contains = true;
break;
}
}
System.out.println("Array contains target: " + contains);
}
}

 Input:
o Array: {9, 7, 5, 3, 1}
o Target: 5
 Output:
o Array contains target: true

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Target number to
target int
search for
Indicates if array has
contains boolean
target
Loop index for
i int
traversing array

5. Find Smallest Element in Array


 Program:
public class LinearSearch5 {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 2, 9};
int smallest = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
System.out.println("Smallest element: " + smallest);
}
}

 Input:
o Array: {5, 3, 8, 2, 9}
 Output:
o Smallest element: 2

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Stores the smallest
smallest int
element found
Loop index for
i int
traversing array

6. Find Largest Element in Array


 Program:
public class LinearSearch6 {
public static void main(String[] args) {
int[] arr = {3, 5, 9, 7, 6};
int largest = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
}
System.out.println("Largest element: " + largest);
}
}

 Input:
o Array: {3, 5, 9, 7, 6}
 Output:
o Largest element: 9

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Stores the largest
largest int
element found
Loop index for
i int
traversing array

7. Find Sum of All Elements


 Program:
public class LinearSearch7 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
System.out.println("Sum of elements: " + sum);
}
}

 Input:
o Array: {1, 2, 3, 4, 5}
 Output:
o Sum of elements: 15

 Variable Description Table:


Variable Type Description
arr int[] Array of integers
Stores the sum of all
sum int
elements
Loop index for
i int
traversing array

Binary Search Programs


1. Binary Search (Iterative Approach)
 Program:

public class BinarySearchIterative {


public static void main(String[] args) {
int[] arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
int target = 11;
int result = binarySearch(arr, target);
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index: " + result);}}
static int binarySearch(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
}
if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
}

 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

2. Binary Search (Recursive Approach)


 Program:
public class BinarySearchRecursive {
public static void main(String[] args) {
int[] arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
int target = 19;
int result = binarySearch(arr, target, 0, arr.length - 1);
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index: " + result);
}
}

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

3. Binary Search with Count of Steps


 Program:
public class BinarySearchWithCount {
public static void main(String[] args) {
int[] arr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
int target = 23;
int[] steps = {0}; // To count the number of steps
int result = binarySearch(arr, target, 0, arr.length - 1, steps);
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index: " + result);
System.out.println("Steps taken: " + steps[0]);
}
}
static int binarySearch(int[] arr, int target, int left, int right, int[] steps) {
steps[0]++;
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, steps);
} else {
return binarySearch(arr, target, left, mid - 1, steps);}}}

 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

4. Binary Search on a Sorted String Array


 Program:

public class BinarySearchString {


public static void main(String[] args) {
String[] arr = {"apple", "banana", "cherry", "date", "fig", "grape"};
String target = "cherry";
int result = binarySearch(arr, target);
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("Element found at index: " + result);
}
}

static int binarySearch(String[] arr, String target) {


int left = 0;
int right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid].equals(target)) {
return mid;
}
if (arr[mid].compareTo(target) < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
}

 Input: arr = {"apple", "banana", "cherry", "date", "fig", "grape"}, target =


"cherry"
 Output: Element found at index: 2

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:

public class BinarySearchFirstOccurrence {


public static void main(String[] args) {
int[] arr = {1, 2, 2, 2, 3, 4, 5};
int target = 2;
int result = binarySearchFirst(arr, target);
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("First occurrence found at index: " + result);
}
}

static int binarySearchFirst(int[] arr, int target) {


int left = 0;
int right = arr.length - 1;
int result = -1;

while (left <= right) {


int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid; // Found target, keep looking left
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}}

 Input: `arr = {1, 2, 2, 2, 3, 4, 5}, target = 2


 Output: 1

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:

public class BinarySearchLastOccurrence {


public static void main(String[] args) {
int[] arr = {1, 2, 2, 2, 3, 4, 5};
int target = 2;
int result = binarySearchLast(arr, target);
if (result == -1) {
System.out.println("Element not found");
} else {
System.out.println("Last occurrence found at index: " + result);
}
}
static int binarySearchLast(int[] arr, int target) {
int left = 0;
int right = arr.length - 1;
int result = -1;

while (left <= right) {


int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid; // Found target, keep looking right
left = mid + 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
}

 Input: arr = {1, 2, 2, 2, 3, 4, 5}, target = 2


 Output: Last occurrence found at index: 3

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

7. Binary Search with Duplicates Count


 Program:

public class BinarySearchCountDuplicates {


public static void main(String[] args) {
int[] arr = {1, 2, 2, 2, 3, 4, 5};
int target = 2;
int count = countOccurrences(arr, target);
System.out.println("Element " + target + " found " + count + " times.");
}

static int countOccurrences(int[] arr, int target) {


int first = binarySearchFirst(arr, target);
int last = binarySearchLast(arr, target);
if (first == -1) {
return 0; // Not found
}
return last - first + 1; // Count of occurrences
}

static int binarySearchFirst(int[] arr, int target) {


int left = 0, right = arr.length - 1, result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid;
right = mid - 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}

static int binarySearchLast(int[] arr, int target) {


int left = 0, right = arr.length - 1, result = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
result = mid;
left = mid + 1;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return result;
}
}

 Input: arr = {1, 2, 2, 2, 3, 4, 5}, target = 2


 Output: Element 2 found 3 times.
Variable Description:
Variable Type Description
arr int[] Array of sorted integers
target int Element to search for
Number of times the target
count int
element appears
Index of the first occurrence of
first int
the target
Index of the last occurrence of
last int
the target

Selection Sort Programs


1. Basic Selection Sort
 Program:

public class SelectionSort {


public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
selectionSort(arr);
System.out.println("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void selectionSort(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}

 Input: arr = {64, 25, 12, 22, 11}


 Output: Sorted array: 11 12 22 25 64

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

2. Selection Sort with Reverse Order


 Program:

public class SelectionSortReverse {


public static void main(String[] args) {
int[] arr = {5, 4, 3, 2, 1};
selectionSortReverse(arr);
System.out.println("Sorted array in reverse order: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void selectionSortReverse(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int maxIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] > arr[maxIndex]) {
maxIndex = j;
}
}
// Swap the found maximum element with the first element
int temp = arr[maxIndex];
arr[maxIndex] = arr[i];
arr[i] = temp;
}
}
}

 Input: arr = {5, 4, 3, 2, 1}


 Output: Sorted array in reverse order: 5 4 3 2 1

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

3. Selection Sort with Negative Numbers


 Program:

public class SelectionSortNegative {


public static void main(String[] args) {
int[] arr = {-1, -3, -2, 0, 2, -4};
selectionSort(arr);
System.out.println("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void selectionSort(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}

 Input: arr = {-1, -3, -2, 0, 2, -4}


 Output: Sorted array: -4 -3 -2 -1 0 2

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

4. Selection Sort for Random Integers


 Program:

import java.util.Random;

public class SelectionSortRandom {


public static void main(String[] args) {
int[] arr = new Random().ints(10, 0, 100).toArray();
System.out.println("Original array: ");
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
selectionSort(arr);
System.out.println("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void selectionSort(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;}}}

 Input: arr (random integers generated)


 Output: Sorted array: [sorted integers]

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

5. Selection Sort with Already Sorted Array


 Program:

public class SelectionSortSorted {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
selectionSort(arr);
System.out.println("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void selectionSort(int[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}

 Input: arr = {1, 2, 3, 4, 5}


 Output: Sorted array: 1 2 3 4 5

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

6. Selection Sort with Floating Point Numbers


 Program:

public class SelectionSortFloat {


public static void main(String[] args) {
float[] arr = {64.5f, 25.1f, 12.3f, 22.8f, 11.9f};
selectionSort(arr);
System.out.println("Sorted array: ");
for (float num : arr) {
System.out.print(num + " ");
}
}

static void selectionSort(float[] arr) {


for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
float temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}

 Input: arr = {64.5f, 25.1f, 12.3f, 22.8f, 11.9f}


 Output: Sorted array: 11.9 12.3 22.8 25.1 64.5

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

7. Selection Sort with Custom Class


 Program:

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;}}}

 Input: students array


 Output: Sorted students by age: Alice - 22, Bob - 20, Charlie – 23

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

Bubble Sort Programs


1. Basic Bubble Sort
 Program:

public class BubbleSort {


public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void bubbleSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}

 Input: arr = {64, 34, 25, 12, 22, 11, 90}


 Output: Sorted array: 11 12 22 25 34 64 90

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

2. Optimized Bubble Sort


 Program:

public class BubbleSortOptimized {


public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("Sorted array: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void bubbleSort(int[] arr) {


int n = arr.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
if (!swapped) break; // Stop if the array is sorted
}
}
}

 Input: arr = {64, 34, 25, 12, 22, 11, 90}


 Output: Sorted array: 11 12 22 25 34 64 90

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

3. Bubble Sort with Reverse Order


 Program:

public class BubbleSortReverse {


public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
bubbleSort(arr);
System.out.println("Sorted array in reverse order: ");
for (int num : arr) {
System.out.print(num + " ");
}
}

static void bubbleSort(int[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}

 Input: arr = {1, 2, 3, 4, 5}


 Output: Sorted array in reverse order: 5 4 3 2 1

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

4. Bubble Sort with Floating Point Numbers


 Program:

public class BubbleSortFloat {


public static void main(String[] args) {
float[] arr = {64.5f, 34.1f, 25.9f, 12.3f, 22.8f};
bubbleSort(arr);
System.out.println("Sorted array: ");
for (float num : arr) {
System.out.print(num + " ");
}
}

static void bubbleSort(float[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j+1]
float temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}

 Input: arr = {64.5f, 34.1f, 25.9f, 12.3f, 22.8f}


 Output: Sorted array: 12.3 22.8 25.9 34.1 64.5

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

5. Bubble Sort with Custom Class


 Program:

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;
}
}}}}

 Input: students array


 Output: Sorted students by age: Alice - 22, Bob - 20, Charlie – 23

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

6. Bubble Sort with Strings


 Program:

public class BubbleSortString {


public static void main(String[] args) {
String[] arr = {"banana", "apple", "kiwi", "orange", "mango"};
bubbleSort(arr);
System.out.println("Sorted array: ");
for (String str : arr) {
System.out.print(str + " ");
}
}

static void bubbleSort(String[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].compareTo(arr[j + 1]) > 0) {
// Swap arr[j] and arr[j+1]
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}

 Input: arr = {"banana", "apple", "kiwi", "orange", "mango"}


 Output: Sorted array: apple banana kiwi mango orange

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

7. Bubble Sort with Case-Insensitive Comparison


 Program:

public class BubbleSortStringCaseInsensitive {


public static void main(String[] args) {
String[] arr = {"Banana", "apple", "kiwi", "Orange", "Mango"};
bubbleSort(arr);
System.out.println("Sorted array: ");
for (String str : arr) {
System.out.print(str + " ");
}
}

static void bubbleSort(String[] arr) {


int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j].compareToIgnoreCase(arr[j + 1]) > 0) {
// Swap arr[j] and arr[j+1]
String temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
}

 Input: arr = {"Banana", "apple", "kiwi", "Orange", "Mango"}


 Output: Sorted array: apple Banana kiwi Mango Orange

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;

public class ReverseString {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String reversed = new StringBuilder(input).reverse().toString();
System.out.println("Reversed string: " + reversed);
scanner.close();
}
}

 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.

2. Count Vowels in a String


 Program:

import java.util.Scanner;

public class CountVowels {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
int count = 0;

for (char c : input.toCharArray()) {


if ("AEIOUaeiou".indexOf(c) != -1) {
count++;
}
}

System.out.println("Number of vowels: " + count);


scanner.close();
}
}
 Input: Hello World
 Output: Number of vowels: 3

Variable Description
Variable Type Description
The string entered by the
input String
user.
The count of vowels in the
count int
input string.

3. Check if a String is Palindrome


 Program:

import java.util.Scanner;

public class PalindromeCheck {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String reversed = new StringBuilder(input).reverse().toString();

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.

4. Count the Number of Words in a String


 Program:

import java.util.Scanner;

public class CountWords {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a sentence: ");
String input = scanner.nextLine();
String[] words = input.split("\\s+");
System.out.println("Number of words: " + words.length);
scanner.close();
}
}

 Input: Hello world this is Java


 Output: Number of words: 6

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.

5. Convert String to Uppercase


 Program:

import java.util.Scanner;

public class ConvertToUppercase {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String uppercase = input.toUpperCase();
System.out.println("Uppercase string: " + uppercase);
scanner.close();
}
}

 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.

6. Find the Length of a String


 Program:

import java.util.Scanner;

public class StringLength {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
System.out.println("Length of the string: " + input.length());
scanner.close();
}
}

 Input: Java Programming


 Output: Length of the string: 16

Variable Description
Variable Type Description
The string entered by the
input String
user.

7. Replace Characters in a String


 Program:

import java.util.Scanner;

public class ReplaceCharacters {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a string: ");
String input = scanner.nextLine();
String replaced = input.replace('a', 'o'); // Replace 'a' with 'o'
System.out.println("Replaced string: " + replaced);
scanner.close();
}
}

 Input: Java is amazing


 Output: Replaced string: Jovo is omozing
Variable Description
Variable Type Description
The string entered by the
input String
user.
The string after character
replaced String
replacement.

You might also like