Skip to the workspace
Pebellslearn by building
Sorting at Scale  ›  Lesson

Why n log n Is a Floor

The Comparison Lower Bound 20 minAny comparison sort is a decision tree with n! leaves, so its worst case needs at least log2(n!) comparisons

dsa / Why n log n Is a Floor

Explore each scene at your own pace.

01 · Count the orderings of a photo lineup

Count the orderings of a photo lineup

Why n log n Is a Floor

Definition
The comparison-sort lower bound says that any algorithm that orders items only by comparing pairs needs at least about n log n comparisons in the worst case, because its decision tree needs a leaf for each of the n! possible orderings.
In plain words
Every comparison is a yes-or-no question, and a sorter has to tell apart every possible shuffle of the items. With that many shuffles to choose from, it cannot get by with fewer questions than the count of shuffles demands, like a game of twenty questions.

In short: any sort that decides order by comparing two items can be drawn as a decision tree — one internal node per comparison, branching yes/no, one leaf per possible final ordering. Sorting n distinct items has n! possible orderings, so the tree needs at least n! leaves, and a binary tree with L leaves needs depth at least ⌈log₂ L⌉. That makes ⌈log₂(n!)⌉ comparisons — which works out to Θ(n log n) — the fewest any comparison sort can need in its worst case. Counting sort isn't a counterexample: it never compares two items against each other, so the argument doesn't apply to it at all.

A small binary decision tree for sorting 3 items: root "a vs b" branches into "b vs c" and "a vs c", and those branch again, ending in 6 leaves — one per ordering of a, b, c — with the deepest leaves 3 comparisons down

step through it
1def permutations(items, chosen):
2 if not items:
3 print(chosen)
4 return
5 for i in range(len(items)):
6 permutations(items[:i] + items[i + 1:], chosen + [items[i]])
7 
8permutations([1, 2, 3], [])

Six lines print, one per leaf of the decision tree in the drawing above — the recursion's call depth is exactly how many choices (comparisons) it takes to pin down one ordering, and the deepest calls go 3 levels in.

Count the orderings of a photo lineup

Quarry's test suite checks a sort against every possible input order for small n, to catch bugs that only show up on rare arrangements. Before it can do that, it needs to know how many orderings exist, and the fewest comparisons any correct sort could need to tell them all apart:

python

Doubling n from 5 to 10 barely doubles min_comparisons (7 to 22) — the log in n log n is why sorting scales so much better than the n! orderings it has to tell apart might suggest.

Scene 1 of 6
Project context and further resources
You're building a piece ofQuarry — the ingest sorter
This piece — sort_lower_bound(): Flags a sort that claims fewer comparisons than are mathematically possible.
Scenario Quarry's test suite wants to know, before it runs a single case, the fewest comparisons any correct sort could possibly need for n items — so it can flag a sort that claims to do better as buggy, not brilliant.
sort_lower_bound.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
sort_lower_bound(n) → dictThe number of orderings of n items, and the fewest comparisons any correct sort could need in its worst case.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.