Skip to content

Why comparison sorts need n log n time

Question

Someone claims a general comparison-based sorting algorithm can run in O(n)O(n) time. How do you show this is impossible?

Short answer

A decision-tree counting argument. Each comparison has two outcomes, so kk comparisons distinguish at most 2k2^k cases, but the algorithm must tell apart all n!n! input orderings. Hence k≥log⁡2n!=Θ(nlog⁡n)k\ge\log_2 n!=\Theta(n\log n) in the worst case. Linear time is only possible with extra structure, such as counting or radix sort on bounded integer keys.

Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q5.11, p.158; question and answer rewritten by SelfMadeQuant.com.

See the hint and the full worked solution in the question bank (free account).

Open the worked solution

Lessons behind this question