∑GateAndNet
PlannerSign Up
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 Θ(n2)\Theta(n^2) while Merge Sort stays rock-solid Θ(nlog⁡n)\Theta(n \log n)!

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

AlgorithmBest CaseAverage CaseWorst CaseMax SwapsAux SpaceStable?Adaptive?
Bubble Sort (Flagged)Θ(n)\Theta(n)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)(n2)\binom{n}{2}O(1)\mathcal{O}(1)✅ Yes✅ Yes
Selection SortΘ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)≤n−1\le n - 1O(1)\mathcal{O}(1)❌ No❌ No
Insertion SortΘ(n)\Theta(n)Θ(n2)\Theta(n^2)Θ(n2)\Theta(n^2)(n2)\binom{n}{2} shiftsO(1)\mathcal{O}(1)✅ Yes✅ Best for nearly sorted!
Merge SortΘ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)— (copies)O(n)\mathcal{O}(n) (array) / O(1)\mathcal{O}(1) (linked list)✅ Yes❌ No
Quick SortΘ(nlog⁡n)\Theta(n \log n)1.39 nlog⁡2n1.39\,n \log_2 nΘ(n2)\Theta(n^2)O(n2)\mathcal{O}(n^2)O(log⁡n)\mathcal{O}(\log n) (tail-rec)❌ No❌ No
Heap SortΘ(nlog⁡n)\Theta(n \log n) (distinct)Θ(nlog⁡n)\Theta(n \log n)Θ(nlog⁡n)\Theta(n \log n)O(nlog⁡n)\mathcal{O}(n \log n)O(1)\mathcal{O}(1)❌ No❌ No
Common Exam Mistake

Three Famous GATE CS Sorting Traps:

  1. Minimum Memory Writes: When flash/EEPROM writes are expensive, Selection Sort (at most n−1n - 1 swaps) beats Insertion, Bubble, Merge, and Quick Sort!
  2. All Elements Identical (A=[5,5,…,5]A = [5, 5, \dots, 5]): Heap Sort finishes in Θ(n)\Theta(n)! Naive Lomuto QuickSort degrades to Θ(n2)\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 mm and nn: m+n−1m + n - 1 (best case: min⁡(m,n)\min(m, n)).
Key Formulas Summary
Inversion Count & Adjacent-Swap Sorting Lower Bound
Max Inversions in n elements=(n2)=n(n−1)2,TInsertion(n)=Θ(n+I)\text{Max Inversions in } n \text{ elements} = \binom{n}{2} = \frac{n(n-1)}{2}, \qquad T_{\text{Insertion}}(n) = \Theta(n + I)
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
TBuild-Heap(n)=∑h=0⌊log⁡2n⌋⌈n2h+1⌉O(h)=O ⁣(n∑h=0∞h2h)=Θ(n)T_{\text{Build-Heap}}(n) = \sum_{h=0}^{\lfloor \log_2 n \rfloor} \left\lceil \frac{n}{2^{h+1}} \right\rceil \mathcal{O}(h) = \mathcal{O}\!\left(n \sum_{h=0}^{\infty} \frac{h}{2^h}\right) = \Theta(n)
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 nn distinct integers is almost sorted: every element is at distance at most kk (k≪nk \ll n, e.g. k=4k = 4) 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 i<ji < j such that A[i]>A[j]A[i] > A[j]) exist in the array A=[8,4,2,1,3,5]A = [8, 4, 2, 1, 3, 5]?

Previous TopicSorting Bounds, Stability, Inversions & P / NP / NP-Complete ReductionsNext Topic Non-Comparison Linear Sorting (Counting, Radix, Bucket) & O(n) Order Statistics