Sub topic 15
The Omega(n log n) Lower Bound for Comparison Sorts
This chapter assumes you know Big-Omega and Big-Theta, the shape of the merge sort and heap sort algorithms, and basic logarithm rules. It proves that no comparison-based sorting algorithm can beat n log n in the worst case. It also shows where that proof stops applying.
What counts as a comparison sort
A comparison sort learns about the input only by asking questions of the form "is a[i] < a[j]?". It never uses the numeric value of a key for anything else, such as indexing an array with it. Insertion, selection, bubble, merge, quick, and heap sort all fit this model.
The bound is a statement about every algorithm in this model, including ones nobody has written yet. The proof therefore cannot analyze a particular algorithm. It analyzes the shape of any algorithm's behavior.
The decision tree model
Fix an algorithm and an input size n. Draw its execution as a binary tree:
- Each internal node is one comparison, such as "a < b?".
- The two branches are the two answers.
- Each leaf is a final claim about the input's order, meaning a permutation of the positions.
- The number of comparisons made on one input is the depth of the leaf that input reaches.
- The worst-case number of comparisons is the height of the tree.
The diagram shows the tree for sorting three distinct keys a, b, c.
The counting argument
Assume the keys are distinct. There are n! possible orderings of the input, and the algorithm must be able to output each one correctly. Two different orderings need different answers, so they must end at different leaves. The tree therefore has at least n! leaves.
A binary tree of height h has at most 2^h leaves. So:
- 2^h ≥ n!
- h ≥ log2(n!)
Now bound log2(n!) from below. The product n! = n · (n-1) · ... · 1 contains at least n/2 factors that are each at least n/2. Hence n! ≥ (n/2)^(n/2), and:
log2(n!) ≥ (n/2) · log2(n/2) = Ω(n log n)
For the matching upper bound, n! ≤ n^n gives log2(n!) ≤ n log2 n. So log2(n!) = Θ(n log n). Stirling's approximation, n! ≈ sqrt(2πn)(n/e)^n, gives the same result.
Every comparison sort therefore needs at least ceil(log2(n!)) comparisons in the worst case, which is Ω(n log n).
Checking the bound against merge sort
The exact lower bound is ceil(log2(n!)). Merge sort's worst case satisfies W(n) = W(floor(n/2)) + W(ceil(n/2)) + n - 1, with W(1) = 0. The program below compares the two.
import math
def lower_bound(n):
# ceil(log2(n!)) computed exactly with integers
return (math.factorial(n) - 1).bit_length()
def merge_worst(n):
if n <= 1:
return 0
return merge_worst(n // 2) + merge_worst(n - n // 2) + n - 1
print("n bound merge")
for n in range(1, 9):
print(n, lower_bound(n), merge_worst(n), sep=" ")It prints these values:
| n | ceil(log2 n!) | Merge sort worst case |
|---|---|---|
| 1 | 0 | 0 |
| 2 | 1 | 1 |
| 3 | 3 | 3 |
| 4 | 5 | 5 |
| 5 | 7 | 8 |
| 6 | 10 | 11 |
| 7 | 13 | 14 |
| 8 | 16 | 17 |
Merge sort matches the bound for n up to 4 and stays within one comparison of it up to n = 8. That is why merge sort and heap sort are called asymptotically optimal comparison sorts. Quick sort has an O(n^2) worst case, so it is not optimal in that sense.
Average case
The average case obeys the same bound. A binary tree with n! leaves has average leaf depth at least log2(n!), because at most 2^k leaves can sit at depth k or less. Most leaves must therefore be deep. For example, fewer than n!/2^(n-1) of the inputs can be sorted in n comparisons or fewer. So no comparison sort averages O(n) comparisons, even if you allow it to be lucky on a tiny share of inputs.
What the bound does not say
- It does not cover non-comparison sorts. Counting sort uses a key as an array index. That gives it more than two outcomes per step, so it is not a binary decision tree. It runs in O(n + k) time, and radix sort in O(d(n + k)). These are linear when k and d are small. See the chapter on counting, radix, and bucket sort.
- It does not forbid fast best cases. Adaptive algorithms can be O(n) on nearly sorted input.
- It does not apply to the model with equal keys unchanged. With many duplicates, the number of distinct outputs falls below n!, and the bound weakens.
- It is a lower bound on comparisons, not on every operation. Data movement is counted separately.
The same technique elsewhere
Any problem whose answer has N possible outcomes, solved by yes/no comparisons, needs at least ceil(log2 N) comparisons. Two examples:
- Searching a sorted array of n keys has n + 1 outcomes: found at one of n positions, or not found. A comparison search therefore needs at least ceil(log2(n + 1)) comparisons, which binary search achieves.
- Finding the maximum has a different lower bound of n - 1. The counting argument gives only log2 n, which is weak. Here you need the argument that every non-maximum element must lose at least one comparison.
The counting argument gives a bound but does not always give the best one.
Exam use
For "minimum comparisons needed to sort n elements in the worst case", compute ceil(log2(n!)). For n = 4 it is ceil(log2 24) = 5. For n = 5 it is ceil(log2 120) = 7. Note that this is a lower bound. Whether an algorithm reaches it is a separate question. For n = 5, a careful algorithm does reach 7, but merge sort needs 8.
Exercise
What is the minimum number of comparisons that any comparison-based algorithm needs in the worst case to sort 5 distinct elements, according to the decision-tree bound?
Show answer
There are 5! = 120 orderings, so the tree needs at least 120 leaves. Since 2^6 = 64 < 120 ≤ 128 = 2^7, the height is at least 7. The bound is ceil(log2 120) = 7 comparisons.
Exercise
Counting sort sorts n integers in the range 0 to k-1 in O(n + k) time. Why does this not contradict the Omega(n log n) bound?
Show answer
The bound applies only to algorithms that gain information solely through pairwise comparisons, which yield two outcomes per step. Counting sort uses each key's value as an array index. A single step can then separate the keys into k classes, so its behavior is not a binary decision tree with height at least log2(n!).
Exercise
Insertion sort takes n - 1 comparisons on an already sorted array. Does this violate the lower bound? Explain.
Show answer
No. The bound is about the worst case. A decision tree has some leaf at depth at least log2(n!), so at least one input needs Omega(n log n) comparisons. The sorted input simply reaches a shallow leaf.