Java Sorting
Friday, January 30, 2026 5:58 PM
Bubble Sort
Idea: Repeatedly swap adjacent elements if they’re in the wrong order
Best for: Very small inputs, learning basics
⏱ Complexity
• Best: O(n) (already sorted)
• Worst/Average: O(n²)
• Space: O(1)
Java Code
class BubbleSort {
public static void main(String[] args) {
int[] arr = {5, 1, 4, 2, 8};
for (int i = 0; i < [Link] - 1; i++) {
for (int j = 0; j < [Link] - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int x : arr)
[Link](x + " ");
}
}
Selection Sort
Idea: Find the minimum element and place it at the beginning
Best for: When memory writes are costly
⏱ Complexity
• Best/Worst/Average: O(n²)
• Space: O(1)
Java Code
class SelectionSort {
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
for (int i = 0; i < [Link] - 1; i++) {
int min = i;
for (int j = i + 1; j < [Link]; j++) {
if (arr[j] < arr[min])
min = j;
}
int temp = arr[min];
arr[min] = arr[i];
arr[i] = temp;
Sorting Page 1
arr[i] = temp;
}
for (int x : arr)
[Link](x + " ");
}
}
Insertion Sort (VERY IMPORTANT)
Idea: Insert each element into its correct position in the sorted part
Best for: Nearly sorted arrays (Capgemini this)
⏱ Complexity
• Best: O(n)
• Worst/Average: O(n²)
• Space: O(1)
Java Code
class InsertionSort {
public static void main(String[] args) {
int[] arr = {9, 5, 1, 4, 3};
for (int i = 1; i < [Link]; 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;
}
for (int x : arr)
[Link](x + " ");
}
}
Merge Sort (MOST ASKED M1 Test)
Idea: Divide array → sort halves → merge
Stable + Efficient
⏱ Complexity
• Best/Worst/Average: O(n log n)
• Space: O(n)
Java Code
class MergeSort {
static void merge(int[] arr, int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int[] L = new int[n1];
int[] R = new int[n2];
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
int i = 0, j = 0, k = l;
Sorting Page 2
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j])
arr[k++] = L[i++];
else
arr[k++] = R[j++];
}
while (i < n1)
arr[k++] = L[i++];
while (j < n2)
arr[k++] = R[j++];
}
static void sort(int[] arr, int l, int r) {
if (l < r) {
int m = (l + r) / 2;
sort(arr, l, m);
sort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
sort(arr, 0, [Link] - 1);
for (int x : arr)
[Link](x + " ");
}
}
Quick Sort
Idea: Pick a pivot, place it correctly, sort left & right
Fastest in practice
⏱ Complexity
• Best/Average: O(n log n)
• Worst: O(n²) (bad pivot)
• Space: O(log n)
Java Code
class QuickSort {
static int partition(int[] arr, int low, int high) {
int pivot = arr[high];
int 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;
Sorting Page 3
return i + 1;
}
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 void main(String[] args) {
int[] arr = {10, 7, 8, 9, 1, 5};
quickSort(arr, 0, [Link] - 1);
for (int x : arr)
[Link](x + " ");
}
}
Java Built-in Sorting (INTERVIEW TIP )
import [Link];
class BuiltInSort {
public static void main(String[] args) {
int[] arr = {5, 2, 8, 1};
[Link](arr); // Dual-Pivot QuickSort
[Link]([Link](arr));
}
}
From <[Link]
From <[Link]
Sorting Page 4