Sorting Algorithms Explained by Someone Who’s Also Still Figuring It Out 😅

Hridish GoswamiHridish Goswami
5 min read

"Bro, why are we even learning sorting when we have sort()?"
— Me, a few months ago

Hey everyone! 👋 I'm Ritam (or just a random 19-year-old trying to survive C++), and today we’re talking about something that sounds boring but is actually weirdly satisfying once you get the hang of it: Sorting Algorithms.

If you’re like me, you probably just blindly used sort(arr, arr+n); in every problem without thinking twice. But when I actually sat down to understand sorting, a few cool things clicked.
So let's dive into the important sorting techniques — explained casually, with code, and no boring textbook vibes.

✨ Why Even Care About Sorting?

Sorting is basically arranging data so that your brain (and your computer) can find stuff faster.
Searching, optimization, efficient storage — almost everything depends on sorted data somewhere.

Plus, interviewers love asking sorting-related questions because they want to see if you can balance speed and memory smartly.

📚 Quick List of Must-Know Sorting Techniques

Sorting TechniqueBest Use CaseVibe Check
Bubble SortLearning basicsCute but slow 😅
Selection SortUnderstand selection logicMid
Insertion SortSmall datasets, nearly sortedNot bad tbh
Merge SortBig datasets, stable sortBig brain
Quick SortMost practical fast sortingZoom zoom
Heap SortTournament trees, priority queuesUnderrated

🚀 Let’s Go Through Them One by One!


🫧 Bubble Sort

"The first thing every CS kid learns. It's slow. It's cute. It's bubble sort."

Idea:
Keep swapping adjacent elements if they’re in the wrong order until the whole array is sorted.

Code:

void bubbleSort(vector<int>& arr) {
    int n = arr.size();
    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], arr[j+1]);
            }
        }
    }
}

Time Complexity:

  • Worst: O(n^2)

  • Best (if optimized for already sorted arrays): O(n)

When to use:

  • Literally never in real life 😂

  • But it’s great for learning "how sorting even works."

    🥴 Selection Sort

    "You basically keep picking the smallest kid from the playground."

    Idea:
    Find the minimum element and move it to its correct position in each iteration.

    Code:

      void selectionSort(vector<int>& arr) {
          int n = arr.size();
          for(int i = 0; i < n-1; i++) {
              int minIdx = i;
              for(int j = i+1; j < n; j++) {
                  if(arr[j] < arr[minIdx])
                      minIdx = j;
              }
              swap(arr[i], arr[minIdx]);
          }
      }
    

    Time Complexity:
    Always O(n^2), no matter what.

    When to use:

    • Never unless someone’s torturing you in an exam.

      ✍️ Insertion Sort

      "Sorting like how you arrange cards in your hand."

      Idea:
      Take one element at a time and insert it in its correct position among the sorted ones.

      Code:

        void insertionSort(vector<int>& arr) {
            int n = arr.size();
            for(int i = 1; i < n; 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;
            }
        }
      

      Time Complexity:

      • Worst: O(n^2)

      • Best: O(n) (when nearly sorted)

When to use:

  • Good for small or nearly sorted arrays!

🧠 Merge Sort

"Divide and conquer, baby!"

Idea:
Split the array into halves, sort them, and then merge them back together.

Code:

        void merge(vector<int>& arr, int l, int m, int r) {
            vector<int> left(arr.begin()+l, arr.begin()+m+1);
            vector<int> right(arr.begin()+m+1, arr.begin()+r+1);
            int i = 0, j = 0, k = l;
            while(i < left.size() && j < right.size()) {
                if(left[i] <= right[j]) arr[k++] = left[i++];
                else arr[k++] = right[j++];
            }
            while(i < left.size()) arr[k++] = left[i++];
            while(j < right.size()) arr[k++] = right[j++];
        }

        void mergeSort(vector<int>& arr, int l, int r) {
            if(l >= r) return;
            int m = l + (r-l)/2;
            mergeSort(arr, l, m);
            mergeSort(arr, m+1, r);
            merge(arr, l, m, r);
        }

Time Complexity:

  • Always O(n log n)

When to use:

  • Large datasets where stability matters (stability = relative order of equal elements preserved).

⚡ Quick Sort

"The unofficial king of sorting in competitive programming."

Idea:
Pick a pivot, partition array around it, and sort partitions recursively.

Code:

        int partition(vector<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++;
                    swap(arr[i], arr[j]);
                }
            }
            swap(arr[i+1], arr[high]);
            return i+1;
        }

        void quickSort(vector<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);
            }
        }

Time Complexity:

  • Average: O(n log n)

  • Worst (already sorted array if pivot is badly chosen): O(n^2)

When to use:

  • Almost always when you need fast sorting (especially randomized pivot quicksort).

🏆 Bonus Mention: Heap Sort

"Sort with the power of heaps and trees!"

Idea:
Convert array into a heap (binary tree) and then repeatedly extract the maximum.

When to use:

  • If you need O(n log n) guaranteed even in worst case, but don’t care about stability.

🌟 Quick Summary Table

AlgorithmBest TimeWorst TimeStable?Practicality
Bubble SortO(n)O(n²)YesNo
Selection SortO(n²)O(n²)NoNo
Insertion SortO(n)O(n²)YesYes (small arrays)
Merge SortO(n log n)O(n log n)YesYes
Quick SortO(n log n)O(n²)NoYes (mostly)
Heap SortO(n log n)O(n log n)NoSometimes

🎯 Final Thoughts

Sorting is one of those things you hate at first but slowly start flexing once you know it.
Learn the basics → Code them out once (yes, manually!) → Then you’ll never forget it. 💪

Thanks for reading!
If you’re also figuring things out like me, drop a comment or connect — let’s mess up code and learn together 😎.

0
Subscribe to my newsletter

Read articles from Hridish Goswami directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hridish Goswami
Hridish Goswami