Why comparison sorts can't beat n log n
About this lesson
Merge sort, heapsort and quicksort on average all land on n log n comparisons. That is no accident: any sort that learns about its items only by comparing them must use about that many. Watch a short Python sort for three items run on 5, 9, 2, draw every path it could take as a decision tree, then count leaves: n items have n! orders, a tree of height h has room for only 2ʰ, so h ≥ log₂(n!), which grows like n log n. The Software lessons on sorting (insertion sort, merge sort and Timsort) and recursion, the Maths lesson on proofs, and "Quicksort and quickselect: partitions and pivots" come first.
A sort that only compares
A comparison sort learns about its items in only one way: it asks “is x ≤ y?” and gets yes or no. Insertion sort, merge sort, quicksort and Python's sorted work like this. Our input: a = 5, b = 9, c = 2.
comparison model: the only way to learn about the items ask x <= y ? -> yes / no input: a = 5, b = 9, c = 2 goal: return the three values in sorted order
Read the lesson step by step
A sort that only compares
A comparison sort learns about its items in only one way: it asks “is x ≤ y?” and gets yes or no. Insertion sort, merge sort, quicksort and Python's sorted work like this. Our input: a = 5, b = 9, c = 2.
comparison model: the only way to learn about the items ask x <= y ? -> yes / no input: a = 5, b = 9, c = 2 goal: return the three values in sorted orderThree items as plain ifs
This Python function sorts exactly three items. Each if asks one question. Each return is one answer: the three items in sorted order. It asks the same questions, in the same order, as insertion sort does on three items.
1 def sort3(a, b, c): 2 if a <= b: 3 if b <= c: return a, b, c 4 if a <= c: return a, c, b 5 return c, a, b 6 if a <= c: return b, a, c 7 if b <= c: return b, c, a 8 return c, b, a sort3(5, 9, 2) returns (2, 5, 9) lines 2, 3, 4, 6, 7 ask a question; every return is an answerQuestion one: is a ≤ b?
Every run starts at the same top question, line 2: is a ≤ b? Here 5 ≤ 9, so the answer is yes and the run follows the yes branch. The input card hops onto the first question.
line 2 a <= b ? 5 <= 9 yes -> line 3 comparisons so far: 1Down the tree to a leaf
Line 3 asks b ≤ c: 9 ≤ 2 is no. Line 4 asks a ≤ c: 5 ≤ 2 is no. Line 5 is a leaf: return c, a, b, which is 2, 5, 9. Three questions sorted this input.
line 2 a <= b ? 5 <= 9 yes line 3 b <= c ? 9 <= 2 no line 4 a <= c ? 5 <= 2 no line 5 return c, a, b -> (2, 5, 9) comparisons: 3Every path is a tree
Each input follows one path like this. Draw all the paths at once and you get a decision tree: 5 questions and 6 leaves. Its height, the longest path from the top to a leaf, is the worst-case number of comparisons: 3.
leaf depth (comparisons) a b c 2 a c b 3 c a b 3 b a c 2 b c a 3 c b a 3 height = longest path = 3Six orders need six leaves
Three different items can arrive in 3! = 3 × 2 × 1 = 6 orders. Each order needs its own leaf. If two orders reached the same leaf, the sort would rearrange both the same way, and one would come out wrong.
the 6 orders of three distinct items, and the leaf each one reaches a=1 b=2 c=3 -> leaf a b c -> 1 2 3 a=1 b=3 c=2 -> leaf a c b -> 1 2 3 a=2 b=1 c=3 -> leaf b a c -> 1 2 3 a=2 b=3 c=1 -> leaf c a b -> 1 2 3 a=3 b=1 c=2 -> leaf b c a -> 1 2 3 a=3 b=2 c=1 -> leaf c b a -> 1 2 3 n! = n × (n - 1) × ... × 1 orders, all different leavesHeight 2 is too short
Each question splits a path in two, so a tree of height h has at most 2ʰ leaves. Height 2 gives at most 4, fewer than 6. So every comparison sort needs 3 questions for some order of three items.
h 2^h >= 6? 0 1 no 1 2 no 2 4 no 3 8 yes smallest h with 2^h >= 3! = 6 is h = 3 so some path must ask a third questionAny n: at least log₂(n!)
The same count works for any n. n items have n! orders, so the tree needs 2ʰ ≥ n!, which means h ≥ log₂(n!). Four items need at least 5 comparisons; ten items need at least 22.
n n! min h 3 6 3 4 24 5 5 120 7 10 3628800 22 min h = smallest h with 2^h >= n! = ⌈log₂(n!)⌉Why log₂(n!) is n log n
At least n/2 of the factors in n! are n/2 or more, so log₂(n!) ≥ (n/2)·log₂(n/2), which grows like n log n. So every comparison sort takes Ω(n log n) comparisons in the worst case (Ω: at least, up to a constant).
n! = 1 × 2 × ... × n >= (n/2)^(n/2) n n/2·lg n/2 lg n! n lg n 4 2.0 4.6 8.0 10 11.6 21.8 33.2 100 282.2 524.8 664.4 1000 4482.9 8529.4 9965.8 lg n! is squeezed between (n/2)·lg(n/2) and n·lg n: Θ(n log n)Who meets it, who escapes
Merge sort and heapsort use O(n log n) comparisons, so they match this floor: no comparison sort beats them by more than a constant factor. Counting sort and radix sort escape it because they never compare two items; they use values as positions.
worst-case comparisons any comparison sort Ω(n log n) (this lesson) merge sort, heapsort O(n log n) optimal insertion sort Θ(n²) counting / radix sort no comparisons: the bound does not apply
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In the decision tree of a comparison sort, what does one leaf stand for?
Each internal node is a question like a ≤ b?. Each leaf is an answer: the order to put the items in, such as c, a, b.
02Why must a tree that sorts 3 distinct items have at least 6 leaves?
If two different orders reached the same leaf, the sort would rearrange both the same way and one of them would come out unsorted.
03A binary tree of height h has at most 2ʰ leaves. What is the fewest comparisons any comparison sort can guarantee for 4 items?
4! = 24 orders need 2ʰ ≥ 24. 2⁴ = 16 is too few and 2⁵ = 32 is enough, so h ≥ 5. Five comparisons really do suffice for 4 items.
04Which step turns h ≥ log₂(n!) into Ω(n log n)?
n! ≥ (n/2)^(n/2) gives log₂(n!) ≥ (n/2)·log₂(n/2), which grows like n log n. The upper bound n! ≤ nⁿ only shows log₂(n!) is at most n log₂ n.
05Counting sort can sort n small integers in O(n + k) time. Why doesn't that contradict this lesson?
The bound only covers algorithms that learn about the items through comparisons. Counting sort uses each value as a position in an array, which is a different kind of question with many possible answers.
Glossary
- Comparison sort
- A sorting algorithm that learns about its items only by comparing two of them at a time, for example asking is a ≤ b?. Insertion sort, merge sort, heapsort, quicksort and Python's sorted are comparison sorts.
- Decision tree
- A drawing of every run of a comparison sort on n items: each internal node is a comparison with a yes and a no branch, and each leaf is a final order.
- Leaf
- A node with no children. In a decision tree it is where a run stops and returns a sorted order.
- Height of a tree
- The number of edges on the longest path from the top (the root) down to a leaf. For a decision tree it is the worst-case number of comparisons.
- n! (n factorial)
- n × (n − 1) × … × 2 × 1, the number of different orders of n distinct items. 3! = 6, 4! = 24, 10! = 3,628,800.
- Lower bound
- A guarantee that no algorithm of a given kind can do better: here, no comparison sort can always finish with fewer than ⌈log₂(n!)⌉ comparisons.
- Asymptotically optimal
- Matching the lower bound up to a constant factor. Merge sort and heapsort are optimal comparison sorts because they use O(n log n) comparisons.
Notes and sources
- The bound counts comparisons only. A comparison sort also moves items, so its running time is at least its number of comparisons: Ω(n log n) time as well. The argument assumes distinct items; they are possible inputs, so the worst case can never be smaller.
- ⌈log₂(n!)⌉ is a floor for every n, not only for large n, but it is not always reached: for 12 items it is 29, while the fewest comparisons that can sort any 12 items is 30.
- The same tree argument bounds the average too: on a random order of n distinct items, no comparison sort averages fewer than log₂(n!) comparisons.
- Python's sorted and list.sort use Timsort, a comparison sort (the Sorting HOWTO says the routines compare with <), so the floor applies. On a list that is already sorted Timsort needs only n − 1 comparisons: the floor is about the worst case.
- The tree drawn here is the decision tree of insertion sort on three items, as in CLRS Figure 8.1. Any comparison sort has a tree like it, possibly with different questions.
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), §8.1 Lower bounds for sorting
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 5: Linear Sorting (comparison sort lower bound)
- Sedgewick and Wayne, Algorithms 4th ed., §2.2 Mergesort (lower bound for compare-based sorting)
- Comparison sort: number of comparisons required to sort a list
- Python documentation: Sorting Techniques (HOWTO)
Quiz Unlocks after the last step.