← All Posts
DSA Series · Complexity · Part 8

Sorting Lower Bounds

We've seen that merge sort and heap sort run in $O(n \log n)$. Can we do better? The answer is no, for comparison-based sorting algorithms. This is one of the most elegant proofs in computer science.

The Question

A comparison-based sorting algorithm can only access elements through comparisons: "is $a_i < a_j$?" It cannot look at the actual values of elements (unlike counting sort or radix sort).

Claim: Any comparison-based sorting algorithm requires $\Omega(n \log n)$ comparisons in the worst case.

The Decision Tree Model

Model any comparison-based algorithm as a binary decision tree:

For the algorithm to be correct, it must be able to produce every possible permutation as output. There are $n!$ permutations of $n$ elements, so the tree must have at least $n!$ leaves.

The Proof

Step 1: Relate tree height to number of leaves

A binary tree of height $h$ has at most $2^h$ leaves. So if the tree has $\geq n!$ leaves:

$$2^h \geq n! \implies h \geq \log_2(n!)$$

Step 2: Apply Stirling's approximation

Stirling's approximation states:

$$n! \approx \sqrt{2\pi n} \left(\frac{n}{e}\right)^n$$

Taking the logarithm:

$$\log_2(n!) = \log_2\left(\sqrt{2\pi n}\right) + n\log_2\left(\frac{n}{e}\right)$$ $$= \frac{1}{2}\log_2(2\pi n) + n\log_2 n - n\log_2 e$$ $$= n\log_2 n - n\log_2 e + O(\log n)$$ $$= \Theta(n \log n)$$

Step 3: Conclude

$$h \geq \log_2(n!) = \Theta(n \log n)$$

The height of the decision tree is the worst-case number of comparisons. Therefore, any comparison-based sorting algorithm requires $\Omega(n \log n)$ comparisons in the worst case. ✔

In plain English: There are $n!$ possible orderings. Each comparison is a yes/no question that halves the remaining possibilities. To narrow down to one ordering, you need at least $\log_2(n!) = \Theta(n \log n)$ questions.

A Simpler Bound (without Stirling)

Even without Stirling, we can show $\log_2(n!) = \Omega(n \log n)$:

$$n! = n \cdot (n-1) \cdot (n-2) \cdots 1 \geq \left(\frac{n}{2}\right)^{n/2}$$

(The top $n/2$ terms are each $\geq n/2$.)

$$\log_2(n!) \geq \frac{n}{2} \cdot \log_2\frac{n}{2} = \frac{n}{2}(\log_2 n - 1) = \Omega(n \log n) \quad \checkmark$$

Complexity of All Major Sorts

AlgorithmBestAverageWorstSpaceStable?Comparison?
Bubble sort$O(n)$$O(n^2)$$O(n^2)$$O(1)$YesYes
Selection sort$O(n^2)$$O(n^2)$$O(n^2)$$O(1)$NoYes
Insertion sort$O(n)$$O(n^2)$$O(n^2)$$O(1)$YesYes
Merge sort$O(n\log n)$$O(n\log n)$$O(n\log n)$$O(n)$YesYes
Quicksort$O(n\log n)$$O(n\log n)$$O(n^2)$$O(\log n)$NoYes
Heap sort$O(n\log n)$$O(n\log n)$$O(n\log n)$$O(1)$NoYes
Counting sort$O(n+k)$$O(n+k)$$O(n+k)$$O(k)$YesNo
Radix sort$O(d(n+k))$$O(d(n+k))$$O(d(n+k))$$O(n+k)$YesNo
Bucket sort$O(n+k)$$O(n+k)$$O(n^2)$$O(n+k)$YesNo

The last three (counting, radix, bucket) are not comparison-based. They exploit the structure of the data (integer values, bounded range) and can beat $O(n \log n)$. The $\Omega(n \log n)$ lower bound only applies to comparison-based sorts.

Which Sorts Are Optimal?

A comparison-based sort is optimal if its worst-case complexity matches the lower bound: $\Theta(n \log n)$.

Breaking the Barrier: Non-Comparison Sorts

Counting sort: $O(n + k)$

If values are integers in $[0, k)$, count occurrences and reconstruct. Time is $O(n + k)$. When $k = O(n)$, this is $O(n)$, beating $n \log n$.

Radix sort: $O(d \cdot (n + k))$

Sort $d$-digit numbers, one digit at a time (using counting sort as a subroutine). For $d = O(\log n)$ and $k = O(n)$, this is $O(n \log n)$. For fixed-width integers (e.g., 32-bit), $d$ is constant, giving $O(n)$.

Why don't we always use radix sort?

Summary