You open your wardrobe.
T-shirts mixed with jeans.
Dresses hiding behind hoodies.
Everything is everywhere. π
You need to sort it.
But here's the catch...
There isn't just one way to organize a messy wardrobe.
And that's exactly what happens in DSA.
An unsorted array is basically a messy wardrobe.
The goal is the same: put everything in order.
But every sorting algorithm has its own strategy.
Let's meet them. π
1. Bubble Sort β Compare the neighbors
Imagine checking your clothes two at a time.
If the two neighboring clothes are in the wrong order:
π Swap them.
Then move to the next pair.
Keep doing this until the largest item gradually reaches the end.
That's why it's called Bubble Sortβelements "bubble" toward their correct positions.
π» Java
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 - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Time: O(nΒ²)
Space: O(1)
π§ Remember:
Bubble = Compare neighbors + Swap
π― 2. Selection Sort β Find the right one
Now you change your strategy.
Instead of repeatedly comparing neighbors, you look through the entire unsorted section.
You find the smallest item.
Then...
π Put it in its correct position.
Repeat.
It's like saying:
"First, I'll find the smallest shirt. Then the next smallest. Then the next..."
That's Selection Sort.
π» Java
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 minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Time: O(nΒ²)
Space: O(1)
π§ Remember:
Selection = Find minimum + Place it
π 3. Insertion Sort β Insert it where it belongs
Now imagine arranging clothes one by one.
You already have a few clothes arranged.
You pick the next one.
Look at the sorted section.
"Where does this belong?"
You make space and insert it there.
Just like arranging playing cards in your hand. π
That's Insertion Sort.
π» Java
public class InsertionSort {
public static void main(String[] args) {
int[] arr = {5, 3, 4, 1, 2};
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;
}
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Best: O(n)
Worst: O(nΒ²)
Space: O(1)
π§ Remember:
Insertion = Take + Shift + Insert
βοΈ 4. Merge Sort β Don't organize everything at once
You look at the wardrobe.
"There are WAY too many clothes. I'm splitting this." π
Divide the clothes into two sections.
Then divide those sections again.
Keep dividing until each section is tiny.
Now sort them.
Then merge the sorted sections back together.
Divide β Sort β Merge
That's Merge Sort.
π» Java
public class MergeSort {
static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
static void merge(int[] arr, int left, int mid, int right) {
int[] temp = new int[right - left + 1];
int i = left;
int j = mid + 1;
int k = 0;
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (int x = 0; x < temp.length; x++) {
arr[left + x] = temp[x];
}
}
public static void main(String[] args) {
int[] arr = {8, 3, 5, 4, 7, 6, 1, 2};
mergeSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Time: O(n log n)
Space: O(n)
π§ Remember:
Merge = Divide + Sort + Merge
β‘ 5. Quick Sort β Choose a pivot
Now you want a faster strategy.
You pick one item as your pivot.
Everything smaller goes to one side.
Everything larger goes to the other.
Now you've created smaller problems.
Repeat the same process.
That's Quick Sort.
π» Java
public class QuickSort {
static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex - 1);
quickSort(arr, pivotIndex + 1, high);
}
}
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;
}
public static void main(String[] args) {
int[] arr = {10, 7, 8, 9, 1, 5};
quickSort(arr, 0, arr.length - 1);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Average: O(n log n)
Worst: O(nΒ²)
Space: O(log n) average recursion stack
π§ Remember:
Quick = Pivot + Partition
π³ 6. Heap Sort β Priority comes first
Now imagine you want the highest-priority clothing item to always be easy to find.
You organize the items using a special structure:
π³ Heap
The largest element can stay at the top of a Max Heap.
Take it.
Put it in its final position.
Restore the heap.
Repeat.
That's Heap Sort.
π» Java
public class HeapSort {
static void heapSort(int[] arr) {
int n = arr.length;
// Build Max Heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// Move maximum to the end
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
static void heapify(int[] arr, int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
heapSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Time: O(n log n)
Space: O(1)
π§ Remember:
Heap = Build heap + Extract repeatedly
π’ 7. Counting Sort β Stop comparing. Start counting.
Here's where things get interesting.
Imagine your clothes can only have sizes:
1, 2, 3, 4, 5
Instead of comparing every item...
You simply count how many times each size appears.
That's the idea behind Counting Sort.
π» Java
public class CountingSort {
static void countingSort(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
int[] count = new int[max + 1];
for (int num : arr) {
count[num]++;
}
int index = 0;
for (int i = 0; i < count.length; i++) {
while (count[i] > 0) {
arr[index] = i;
index++;
count[i]--;
}
}
}
public static void main(String[] args) {
int[] arr = {4, 2, 2, 8, 3, 3, 1};
countingSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Time: O(n + k)
Space: O(k)
π§ Remember:
Counting = Count frequencies
β οΈ Works well when the range of values is reasonably small.
π 8. Radix Sort β One digit at a time
Now imagine numbers are your clothes.
Instead of comparing the whole number...
You organize them digit by digit.
First:
β‘οΈ Ones digit
Then:
β‘οΈ Tens digit
Then:
β‘οΈ Hundreds digit
For example:
170, 45, 75, 90, 802, 24, 2, 66
The algorithm gradually builds the final ordering.
That's Radix Sort.
π» Java
public class RadixSort {
static void radixSort(int[] arr) {
int max = arr[0];
for (int num : arr) {
if (num > max) {
max = num;
}
}
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSort(arr, exp);
}
}
static void countingSort(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
for (int num : arr) {
int digit = (num / exp) % 10;
count[digit]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
for (int i = n - 1; i >= 0; i--) {
int digit = (arr[i] / exp) % 10;
output[count[digit] - 1] = arr[i];
count[digit]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
public static void main(String[] args) {
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
radixSort(arr);
for (int num : arr) {
System.out.print(num + " ");
}
}
}
Time: O(d Γ (n + k))
Space: O(n + k)
π§ Remember:
Radix = Digit by digit
πͺ£ 9. Bucket Sort β Group first, sort later
One last strategy.
Instead of throwing every piece of clothing into one giant pile...
You create buckets.
πͺ£ Bucket 1 β small values
πͺ£ Bucket 2 β medium values
πͺ£ Bucket 3 β large values
Then sort each bucket individually.
Finally...
Combine them.
That's Bucket Sort.
π» Java
import java.util.*;
public class BucketSort {
static void bucketSort(float[] arr) {
int n = arr.length;
List<Float>[] buckets = new ArrayList[n];
for (int i = 0; i < n; i++) {
buckets[i] = new ArrayList<>();
}
for (float num : arr) {
int index = (int) (num * n);
buckets[index].add(num);
}
for (List<Float> bucket : buckets) {
Collections.sort(bucket);
}
int index = 0;
for (List<Float> bucket : buckets) {
for (float num : bucket) {
arr[index++] = num;
}
}
}
public static void main(String[] args) {
float[] arr = {
0.42f, 0.32f, 0.23f,
0.52f, 0.25f, 0.47f, 0.51f
};
bucketSort(arr);
for (float num : arr) {
System.out.print(num + " ");
}
}
}
Average:O(n + k)
Worst: O(nΒ²)
π§ Remember:
Bucket = Distribute β Sort β Combine
π§ So... which algorithm would YOU use?
Now look back at your messy wardrobe.
Same problem.
Different strategies.
| Algorithm | Wardrobe Strategy | Main Idea |
| Bubble | Compare neighbors | Swap |
| π― Selection | Find the right item | Select |
| π Insertion | Place one item at a time | Insert |
| βοΈ Merge | Split the wardrobe | Divide & Merge |
| β‘ Quick | Pick a reference item | Pivot |
| π³ Heap | Prioritize items | Heap |
| π’ Counting | Count categories | Frequency |
| π Radix | Sort digit by digit | Digits |
| πͺ£ Bucket | Make groups first | Distribution |
And here's the part I want you to remember:
Sorting isn't just about arranging numbers.
It's about learning that the same problem can have completely different solutions.
And in DSA, the real question isn't:
"Can I sort this?"
It's:
"How efficiently can I sort this?" π
That's where Time Complexity enters the story.
Because if your wardrobe has 10 clothes, almost anything works.
But what if it has 10 lakh clothes? π
Suddenly, your choice of algorithm matters.