Knowledge / Computer Science and Engineering / Sorting algorithms

Sorting algorithms

Computer Science and Engineering (B.E./B.Tech) · Algorithms · free study notes for exams, placements and interviews

In short

Sorting arranges records in non-decreasing (or non-increasing) order of a key, and it is the most-examined single topic in data structures and algorithms because almost every other technique — binary search, grouping, deduplication, two-pointer and sliding-window solutions, database joins — assumes sorted input. A standard syllabus covers the three elementary O(n²) sorts, the three O(n log n) comparison sorts, the Ω(n log n) comparison lower bound, and the linear-time counting/radix/bucket sorts that beat it by not comparing keys. GATE CS asks it almost every year (trace a pass, count comparisons or swaps, identify stability, recognise a worst case), and placement rounds at TCS NQT, Infosys, Wipro, Cognizant, Zoho, Amazon India, Flipkart, Paytm and Zerodha ask it in both written and whiteboard form. In production you rarely write a sort — you choose one, and you must justify the choice on stability, memory, data distribution and worst-case guarantee.

Explained

Definitions and classification

Elementary O(n²) sorts

The O(n log n) comparison sorts

Linear-time sorts: counting, radix, bucket

The Ω(n log n) lower bound and special cases

External sorting and choosing an algorithm

Key points

Formulas and examples

Comparisons, bubble and selection sort = n(n−1)/2 ; maximum inversions = n(n−1)/2
Insertion sort: shifts = I (number of inversions); comparisons = I + (number of keys that stop inside the prefix); best = n−1
Merge sort worst-case comparisons: T(n) = 2T(n/2) + (n−1), T(1)=0 → T(n) = n log₂n − n + 1 (n a power of 2)
Quicksort average comparisons C(n) = 2(n+1)H(n) − 4n ≈ 1.386 n log₂n, where H(n) = 1 + 1/2 + … + 1/n ; worst = n(n−1)/2
Heap sort ≤ 2n log₂n comparisons; build-heap = Θ(n); extraction = Σ log i = Θ(n log n)
Comparison lower bound: h ≥ log₂(n!) ≈ n log₂n − 1.4427n
Counting sort = Θ(n + k), k = key range ; Radix sort = Θ(d(n + b)), d = ⌈log_b(max key + 1)⌉, b = base
External merge sort passes = 1 + ⌈log_m(N/M)⌉ ; N = file size, M = memory size, m = merge order
Heap indices (0-based): parent ⌊(i−1)/2⌋, children 2i+1, 2i+2 ; heapify from i = ⌊n/2⌋−1 down to 0

Example 1 — Insertion sort trace, comparisons and inversions on [5, 2, 4, 6, 1, 3]

ikeyarray after insertioncomparisonsshifts
122 5 4 6 1 311
242 4 5 6 1 321
362 4 5 6 1 310
411 2 4 5 6 344
531 2 3 4 5 643

Example 2 — Merge sort comparisons for n = 8, against the lower bound

Example 3 — Lomuto partition on [8, 3, 5, 1, 9, 4, 7], pivot = last = 7

i = -1, pivot = 7 (index 6)
j=0: 8 > 7            -> 8 3 5 1 9 4 7
j=1: 3 <= 7, i=0, swap-> 3 8 5 1 9 4 7
j=2: 5 <= 7, i=1, swap-> 3 5 8 1 9 4 7
j=3: 1 <= 7, i=2, swap-> 3 5 1 8 9 4 7
j=4: 9 > 7            -> 3 5 1 8 9 4 7
j=5: 4 <= 7, i=3, swap-> 3 5 1 4 9 8 7
swap a[i+1]=a[4] with a[6] -> 3 5 1 4 | 7 | 8 9

Example 4 — Counting sort on A = [2, 5, 3, 0, 2, 3, 0, 3], k = 5

Example 5 — Radix sort: choosing the base for 1,00,000 keys < 10⁶

Example 6 — External sort of a 100 GB file with 4 GB usable memory, 16-way merge

AlgorithmBestAverageWorstExtra spaceStableIn-place
Bubble (with flag)Θ(n)Θ(n²)Θ(n²)O(1)YesYes
SelectionΘ(n²)Θ(n²)Θ(n²)O(1)NoYes
InsertionΘ(n)Θ(n²)Θ(n²)O(1)YesYes
Shell (Knuth gaps)Θ(n log n)~Θ(n1.25–1.5)Θ(n1.5)O(1)NoYes
MergeΘ(n log n)Θ(n log n)Θ(n log n)Θ(n)YesNo
Quick (randomised)Θ(n log n)Θ(n log n)Θ(n²)O(log n) stackNoYes
HeapΘ(n log n)*Θ(n log n)Θ(n log n)O(1)NoYes
CountingΘ(n+k)Θ(n+k)Θ(n+k)Θ(n+k)YesNo
Radix (LSD)Θ(d(n+b))Θ(d(n+b))Θ(d(n+b))Θ(n+b)YesNo
BucketΘ(n+k)Θ(n+k)Θ(n²)Θ(n)YesNo

*Heap sort is Θ(n) only in the degenerate case of all-equal keys.

Where it is used

Interview questions

Common mistakes

Related topics

Time and space complexity · Divide and conquer · Searching and binary search · Heaps and priority queues · DSA interview patterns · Hashing and database indexing

Open in the Knowledge reader →Find jobs →