LOGBOOK

HELP

Quiz Entry - updated: 2026.12.02

Why does merge-sort run in $O(n \log n)$ time?

Each call halves its sequence, so the tree has $O(\log n)$ levels; and the cuts and merges on any one level together handle every element once, which is $O(n)$ per level. $O(\log n)$ levels times $O(n)$ per level gives $O(n \log n)$.

The sorted pieces at each depth of the merge-sort tree for 7 2 9 4 3 8 6 1: one piece of 8, two of 4, four of 2, eight of 1

* Every row holds all eight numbers: the pieces get smaller, the total per level stays $n$. *

Count the work per level instead of per call. At depth $i$ there are $2^i$ sequences, each of size $n/2^i$. Cutting and merging a sequence of length $m$ costs $O(m)$, so the whole level costs $2^i \cdot O(n/2^i) = O(n)$, the same at every depth. Halving reaches size 1 after $\log_2 n$ steps, so there are about $\log_2 n$ levels of merges. Multiplied: $O(n \log n)$.

This bound holds for every input, sorted, reversed or random, because the cuts ignore the values. That sets merge-sort apart from insertion-sort (fast on nearly sorted input, $O(n^2)$ on reversed input) and from quick-sort, whose speed depends on its pivots.

For a feel of the numbers: with 1,000 elements a quadratic sort does about $1000^2$ = 1,000,000 steps, merge-sort about $1000 \cdot 10$ = 10,000, since $1000 \approx 2^{10}$. That is a factor of 100. With 2,000 elements it is 4,000,000 against $2000 \cdot 11$ = 22,000, already a factor of about 180, and the gap keeps widening with every doubling.

Go deeper:

From Quiz: ADS / Merge-Sort and Quick-Sort | Updated: Dec 02, 2026