# Famous Sorting Algorithms (Animated): Bubble, Selection, Insertion, Merge, Quick & Heap Sort
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Algorithms
Canonical URL: https://gateandnet.com/exams/gate-cs/algorithms/famous-comparison-sorting-algorithms-animated

> Interactive animated traces, exact comparison/swap counts, inversion bounds, Lomuto vs Hoare QuickSort partitioning, Merge Sort divide-and-conquer, and O(n) Build-Heap proof.

## 1. Interactive Live Sorting Animation (Quick, Merge, Insertion, Selection, Bubble)

Switch between **Quick, Merge, Insertion, Selection, and Bubble Sort** right inside the interactive stage below — try clicking **Reverse-Sorted (Worst)** to watch QuickSort degrade to $\Theta(n^2)$ while Merge Sort stays rock-solid $\Theta(n \log n)$!

```visualizer
quick
```

## 2. Exact Comparison, Swap & Stability Master Matrix

| Algorithm | Best Case | Average Case | Worst Case | Max Swaps | Aux Space | Stable? | Adaptive? |
| :--- | :---: | :---: | :---: | :---: | :---: | :---: | :---: |
| **Bubble Sort (Flagged)** | $\Theta(n)$ | $\Theta(n^2)$ | $\Theta(n^2)$ | $\binom{n}{2}$ | $\mathcal{O}(1)$ | ✅ Yes | ✅ Yes |
| **Selection Sort** | $\Theta(n^2)$ | $\Theta(n^2)$ | $\Theta(n^2)$ | **$\le n - 1$** | $\mathcal{O}(1)$ | ❌ No | ❌ No |
| **Insertion Sort** | $\Theta(n)$ | $\Theta(n^2)$ | $\Theta(n^2)$ | $\binom{n}{2}$ shifts | $\mathcal{O}(1)$ | ✅ Yes | ✅ **Best for nearly sorted!** |
| **Merge Sort** | $\Theta(n \log n)$ | $\Theta(n \log n)$ | $\Theta(n \log n)$ | — (copies) | $\mathcal{O}(n)$ *(array)* / $\mathcal{O}(1)$ *(linked list)* | ✅ Yes | ❌ No |
| **Quick Sort** | $\Theta(n \log n)$ | $1.39\,n \log_2 n$ | $\Theta(n^2)$ | $\mathcal{O}(n^2)$ | $\mathcal{O}(\log n)$ *(tail-rec)* | ❌ No | ❌ No |
| **Heap Sort** | $\Theta(n \log n)$ *(distinct)* | $\Theta(n \log n)$ | $\Theta(n \log n)$ | $\mathcal{O}(n \log n)$ | $\mathcal{O}(1)$ | ❌ No | ❌ No |

> [!TRAP]
> **Three Famous GATE CS Sorting Traps:**
> 1. **Minimum Memory Writes:** When flash/EEPROM writes are expensive, **Selection Sort** (at most $n - 1$ swaps) beats Insertion, Bubble, Merge, and Quick Sort!
> 2. **All Elements Identical ($A = [5, 5, \dots, 5]$):** Heap Sort finishes in **$\Theta(n)$**! Naive Lomuto QuickSort degrades to **$\Theta(n^2)$** unless 3-way Dutch National Flag partitioning (or Hoare partition) is used!
> 3. **Worst-Case Merge Comparisons** to merge two sorted lists of sizes $m$ and $n$: **$m + n - 1$** (best case: $\min(m, n)$).

## Solved Practice Questions & PYQs

### [GATE CS 2025 | MCQ]
An array of $n$ distinct integers is **almost sorted**: every element is at distance **at most $k$** ($k \ll n$, e.g. $k = 4$) from its final sorted index. Which sorting algorithm achieves the tightest worst-case asymptotic runtime?
**Correct Answer:** B
**Solution:** Each element participates in at most $k$ inversions ($I \le nk$), so **Insertion Sort** runs in $\Theta(n + I) = \mathcal{O}(nk) = \Theta(n)$ for constant $k$, and a sliding **Min-Heap of size $k+1$** runs in $\mathcal{O}(n \log k)$! (Trap: Last-element QuickSort on nearly sorted input approaches $\Theta(n^2)$!)

### [GATE CS 2023 | NAT]
How many **inversions** (pairs $i < j$ such that $A[i] > A[j]$) exist in the array $A = [8, 4, 2, 1, 3, 5]$?
**Correct Answer:** 9
**Solution:** - `8` $> \{4,2,1,3,5\}$ ($5$)
- `4` $> \{2,1,3\}$ ($3$)
- `2` $> \{1\}$ ($1$)
- `1, 3, 5` contribute $0$.
Total inversions $= 5 + 3 + 1 = \mathbf{9}$.
