Here is a complete list of searching and sorting algorithms in Java, each with:
✅ Code
📌 Explanation
🧾 Sample Input & Output
🔍 SEARCHING ALGORITHMS
1. Linear Search
public class LinearSearch {
public static void main(String[] args) {
int[] arr = {10, 30, 50, 70, 90};
int target = 50;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
System.out.println("Element found at index: " + i);
found = true;
break;
}
}
if (!found) System.out.println("Element not found.");
}
}
📤 Output:
Element found at index: 2
2. Binary Search (Array must be sorted)
import java.util.Arrays;
public class BinarySearch {
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50};
int target = 40;
int left = 0, right = arr.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
System.out.println("Element found at index: " + mid);
return;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
System.out.println("Element not found.");
}
}
📤 Output:
Element found at index: 3
🔁 SORTING ALGORITHMS
1. Bubble Sort
import java.util.Arrays;
public class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 3, 8, 4, 2};
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
📤 Output:
Sorted Array: [2, 3, 4, 5, 8]
2. Selection Sort
import java.util.Arrays;
public class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
for (int i = 0; i < arr.length - 1; i++) {
int minIdx = i;
for (int j = i+1; j < arr.length; j++) {
if (arr[j] < arr[minIdx])
minIdx = j;
}
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
📤 Output:
Sorted Array: [11, 12, 22, 25, 64]
3. Insertion Sort
import java.util.Arrays;
public class InsertionSort {
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6};
for (int i = 1; i < arr.length; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j+1] = arr[j];
j--;
}
arr[j+1] = key;
}
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
📤 Output:
Sorted Array: [5, 6, 11, 12, 13]
4. Merge Sort
import java.util.Arrays;
public class MergeSort {
public static void mergeSort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
}
public static void merge(int[] arr, int l, int m, int r) {
int[] left = Arrays.copyOfRange(arr, l, m+1);
int[] right = Arrays.copyOfRange(arr, m+1, r+1);
int i = 0, j = 0, k = l;
while (i < left.length && j < right.length) {
if (left[i] <= right[j])
arr[k++] = left[i++];
else
arr[k++] = right[j++];
}
while (i < left.length) arr[k++] = left[i++];
while (j < right.length) arr[k++] = right[j++];
}
public static void main(String[] args) {
int[] arr = {38, 27, 43, 3, 9};
mergeSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
📤 Output:
Sorted Array: [3, 9, 27, 38, 43]
5. Quick Sort
import java.util.Arrays;
public class QuickSort {
public static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
public static int partition(int[] arr, int low, int high) {
int pivot = arr[high], i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] <= pivot) {
i++;
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
}
}
int temp = arr[i+1]; arr[i+1] = arr[high]; arr[high] = temp;
return i + 1;
}
public static void main(String[] args) {
int[] arr = {10, 7, 8, 9, 1, 5};
quickSort(arr, 0, arr.length - 1);
System.out.println("Sorted Array: " + Arrays.toString(arr));
}
}
📤 Output:
Sorted Array: [1, 5, 7, 8, 9, 10]
✅ Updated! The PDF now includes additional sorting algorithms:
Heap Sort
Counting Sort
Radix Sort
Shell Sort
📦 1. Heap Sort
public class HeapSort {
public void sort(int arr[]) {
int n = arr.length;
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// Heap sort
for (int i = n - 1; i > 0; i--) {
// Swap
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// Heapify root element
heapify(arr, i, 0);
}
}
void heapify(int arr[], int n, int i) {
int largest = i;
int l = 2 * i + 1; // left
int r = 2 * i + 2; // right
if (l < n && arr[l] > arr[largest]) largest = l;
if (r < n && arr[r] > arr[largest]) largest = r;
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
heapify(arr, n, largest);
}
}
public static void main(String args[]) {
int arr[] = {12, 11, 13, 5, 6, 7};
HeapSort hs = new HeapSort();
hs.sort(arr);
System.out.println("Sorted array: " +
java.util.Arrays.toString(arr));
}
}
🧾 Output:
Sorted array: [5, 6, 7, 11, 12, 13]
🔢 2. Counting Sort
public class CountingSort {
void sort(int arr[]) {
int max = arr[0];
for (int i : arr) if (i > max) max = i;
int[] count = new int[max + 1];
for (int i : arr) count[i]++;
int index = 0;
for (int i = 0; i < count.length; i++) {
while (count[i]-- > 0)
arr[index++] = i;
}
}
public static void main(String args[]) {
int arr[] = {4, 2, 2, 8, 3, 3, 1};
CountingSort cs = new CountingSort();
cs.sort(arr);
System.out.println("Sorted array: " +
java.util.Arrays.toString(arr));
}
}
🧾 Output:
Sorted array: [1, 2, 2, 3, 3, 4, 8]
📍 3. Radix Sort
public class RadixSort {
static int getMax(int arr[]) {
int max = arr[0];
for (int i : arr)
if (i > max) max = i;
return max;
}
static void countSort(int arr[], int exp) {
int output[] = new int[arr.length];
int count[] = new int[10];
for (int i : arr)
count[(i / exp) % 10]++;
for (int i = 1; i < 10; i++)
count[i] += count[i - 1];
for (int i = arr.length - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
System.arraycopy(output, 0, arr, 0, arr.length);
}
static void radixSort(int arr[]) {
int m = getMax(arr);
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, exp);
}
public static void main(String[] args) {
int arr[] = {170, 45, 75, 90, 802, 24, 2, 66};
radixSort(arr);
System.out.println("Sorted array: " +
java.util.Arrays.toString(arr));
}
}
🧾 Output:
Sorted array: [2, 24, 45, 66, 75, 90, 170, 802]
🌀 4. Shell Sort
public class ShellSort {
public static void shellSort(int arr[]) {
int n = arr.length;
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j = i;
while (j >= gap && arr[j - gap] > temp) {
arr[j] = arr[j - gap];
j -= gap;
}
arr[j] = temp;
}
}
}
public static void main(String[] args) {
int arr[] = {12, 34, 54, 2, 3};
shellSort(arr);
System.out.println("Sorted array: " +
java.util.Arrays.toString(arr));
}
}
🧾 Output:
Sorted array: [2, 3, 12, 34, 54]