Algorithms · Sorting Beyond the Lecture: Linear-Time Sorts, Lower Bound, Selection and the Full Comparison
The Comparison Lower Bound: Ω(n log n)
4.1 The decision-tree model
Any comparison sort that only asks "is ?" can be drawn as a binary decision tree: each internal node is one comparison (two outcomes), each leaf is a permutation of the input that the algorithm outputs.
- The algorithm must be correct on all possible orderings of distinct keys, so the tree needs at least leaves.
- A binary tree of height has at most leaves. Hence , i.e.
- The worst-case number of comparisons is the height , so every comparison sort needs at least comparisons in the worst case.
4.2 Bounding
(Stirling's formula gives the sharper .)
Conclusion: comparison sorting is in the worst case, and merge sort / heap sort achieve it, so they are asymptotically optimal. This bound does not apply to counting, radix or bucket sort — they never compare keys with each other; they use the keys as array indices or arithmetic.
4.3 Minimum comparisons to sort small inputs
The lower bound is a lower bound; for tiny the true optimum is known exactly.
Sort 4 elements: 5 comparisons suffice, and 4 cannot. With 4 elements there are orderings; , so at least 5 comparisons are needed, and a 5-comparison strategy exists. Answer: 5.
Sort 5 elements: 7 comparisons suffice, and 6 cannot. and , so at least 7; a 7-comparison strategy exists. Answer: 7.
(These two numbers — 5 and 7 — are the standard GATE answers. Note the information-theoretic bound is tight here.)