1. Gyanastra
  2. Algorithms
  3. Sorting Beyond the Lecture: Linear-Time Sorts, Lower Bound, Selection and the Full Comparison
  4. The Comparison Lower Bound: Ω(n log n)
Free lesson, no account needed 4 min read
Asked in past papers4 times
  • GATE CSE20041991
  • UGC NET20172014

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 A[i]<A[j]A[i] < A[j]?" 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 n!n! possible orderings of nn distinct keys, so the tree needs at least n!n! leaves.
  • A binary tree of height hh has at most 2h2^h leaves. Hence 2h≥n!2^h \ge n!, i.e.

h≥log⁡2(n!).h \ge \log_2(n!).

  • The worst-case number of comparisons is the height hh, so every comparison sort needs at least log⁡2(n!)\log_2(n!) comparisons in the worst case.

4.2 Bounding log⁡2(n!)\log_2(n!)

log⁡2(n!)=∑i=1nlog⁡2i  ≥  ∑i=⌈n/2⌉nlog⁡2i  ≥  n2log⁡2n2=Θ(nlog⁡n).\log_2(n!) = \sum_{i=1}^{n}\log_2 i \;\ge\; \sum_{i=\lceil n/2\rceil}^{n}\log_2 i \;\ge\; \frac{n}{2}\log_2\frac{n}{2} = \Theta(n\log n).

(Stirling's formula gives the sharper log⁡2(n!)=nlog⁡2n−nlog⁡2e+O(log⁡n)≈nlog⁡2n−1.44n\log_2(n!) = n\log_2 n - n\log_2 e + O(\log n) \approx n\log_2 n - 1.44n.)

Conclusion: comparison sorting is Ω(nlog⁡n)\Omega(n\log n) 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 nn the true optimum is known exactly.

Sort 4 elements: 5 comparisons suffice, and 4 cannot. With 4 elements there are 4!=244! = 24 orderings; ⌈log⁡224⌉=5\lceil\log_2 24\rceil = 5, so at least 5 comparisons are needed, and a 5-comparison strategy exists. Answer: 5.

Sort 5 elements: 7 comparisons suffice, and 6 cannot. 5!=1205! = 120 and ⌈log⁡2120⌉=7\lceil\log_2 120\rceil = 7, 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.)


Pages 1–2 of 2