INTERMEDIATEFrequently Tested 4 min read 4 views
Famous Sorting Algorithms (Animated): Bubble, Selection, Insertion, Merge, Quick & Heap Sort
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 while Merge Sort stays rock-solid !
Interactive Step-by-Step Animation
Quick Sort (Lomuto Partition)
Avg Θ(n log n) • Worst O(n²)Space: O(log n) stackUnstableGATE CS Favorite
Comparisons: 0Swaps / Writes: 0
38[0]
14[1]
52[2]
23[3]
67[4]
19[5]
44[6]
11[7]
31[8]
Initial array of 9 elements ready for QUICK trace.
1 / 42
2. Exact Comparison, Swap & Stability Master Matrix
| Algorithm | Best Case | Average Case | Worst Case | Max Swaps | Aux Space | Stable? | Adaptive? |
|---|---|---|---|---|---|---|---|
| Bubble Sort (Flagged) | ✅ Yes | ✅ Yes | |||||
| Selection Sort | ❌ No | ❌ No | |||||
| Insertion Sort | shifts | ✅ Yes | ✅ Best for nearly sorted! | ||||
| Merge Sort | — (copies) | (array) / (linked list) | ✅ Yes | ❌ No | |||
| Quick Sort | (tail-rec) | ❌ No | ❌ No | ||||
| Heap Sort | (distinct) | ❌ No | ❌ No |
Common Exam Mistake
Three Famous GATE CS Sorting Traps:
- Minimum Memory Writes: When flash/EEPROM writes are expensive, Selection Sort (at most swaps) beats Insertion, Bubble, Merge, and Quick Sort!
- All Elements Identical (): Heap Sort finishes in ! Naive Lomuto QuickSort degrades to unless 3-way Dutch National Flag partitioning (or Hoare partition) is used!
- Worst-Case Merge Comparisons to merge two sorted lists of sizes and : (best case: ).
Key Formulas Summary
Inversion Count & Adjacent-Swap Sorting Lower Bound
Note: Every adjacent swap removes at most 1 inversion => Insertion & Bubble Sort run in O(n) when I = O(n)!
Build-Heap Tight Linear Bound vs Heap Sort Total Time
Note: Since sum (h / 2^h) = 2, Build-Max-Heap is O(n), while n Extract-Max steps take Theta(n log n)!
#Algorithms#Sorting#Quick Sort#Merge Sort#Heap Sort#Insertion Sort#Inversions#GATE CS
Practice Questions on This Chapter (2)
Q1GATE CS 2025MCQMEDIUM
+2M • -0.67
An array of distinct integers is almost sorted: every element is at distance at most (, e.g. ) from its final sorted index. Which sorting algorithm achieves the tightest worst-case asymptotic runtime?
⚡ Click an option above to verify instantly
Q2GATE CS 2023NATEASY
+1M • 0 Neg
How many inversions (pairs such that ) exist in the array ?