Quick-sort has an $O(n^2)$ worst case and merge-sort never does. Why is quick-sort still usually the faster one?
Its inner loop is tiny (compare with the pivot, move an index, swap now and then), it works in place on contiguous memory, which suits the CPU cache, and it never copies the data into a buffer and back. With a random or middle pivot the quadratic case is so improbable that these constant factors decide.
The $O$-notation hides constant factors, and here they are what counts. Quick-sort even makes more comparisons on average, about $1.39\, n \log_2 n$ against at most about $n \log_2 n$ for merge-sort, and still finishes first, because each of its steps is cheaper:
- Data movement: merge-sort writes every element into a buffer on every level, $n \log_2 n$ moves in total. Quick-sort only swaps the elements that are on the wrong side of the pivot.
- Memory access: both partition scans walk the array sequentially, which the CPU cache and its prefetcher handle at full speed, and there is no second array competing for the cache.
- Bookkeeping: no allocation of buffers, no copying back; the recursion works on index ranges.
Merge-sort keeps its place where its own strengths count: a guaranteed $O(n \log n)$, stability, and sequential access for linked lists and external data.
Go deeper:
Merge sort: comparison with other sort algorithms โ where quick-sort wins (arrays in memory) and where merge-sort does (stability, linked lists, slow sequential media).