What if your wardrobe could teach you Sorting Algorithms?πŸ‘—

●1 ●2 ●10
calendar_today β€’ schedule4 min read

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.

Want more #dsa & #codingjourney deep dives?

Join 6,176+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

2 Comments

1 vote
0

More Posts

Cisco's Amy Chang: A Model's "Passport" Doesn't Tell You Where It Actually Came From

Tom Smithverified - Aug 27

Your Backup Data Knows More Than You Think. HYCU aiR Is Finally Asking It the Right Questions.

Tom Smithverified - May 14

What Security Teams Want From AI Agents Before They Let Them Act Alone

Tom Smithverified - Nov 18

AAIF Hits 10 Projects. Its CTO on What to Adopt Now and the Security Gaps Left Open

Tom Smithverified - Oct 23

Your Tech Stack Isn’t Your Ceiling. Your Story Is

Karol Modelski - Apr 9
chevron_left
908 Points β€’ 13 Badges
Chennai
3Posts
2Comments
8Connections
Building solutions with curiosity, engineering with purpose.
Full Stack Developer β€’ AI Enthusiast β€’ Open to collaboration ?

Related Jobs

View all jobs β†’

Commenters (This Week)

13 comments
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!