Merge Sort vs Quick Sort
Across our 3 test inputs, Merge Sort needed 59 comparisons in total versus 106 for Quick Sort. Merge Sort is stable; Quick Sort is not.
Head-to-head on identical inputs (comparisons)
| Input | Merge Sort | Quick Sort |
|---|---|---|
| Random order | 25 | 22 |
| Nearly sorted | 19 | 39 |
| Reversed | 15 | 45 |
Complexity
| Merge Sort | Quick Sort | |
|---|---|---|
| Average time | O(n log n) | O(n log n) |
| Worst time | O(n log n) | O(n²) |
| Space | O(n) | O(log n) |
| Stable | Yes | No |
When to use Merge Sort
When stable sorting is strictly required and O(n) extra space is acceptable. Highly predictable performance (always O(n log n)).
When to use Quick Sort
General-purpose sorting. Fast in practice due to excellent cache locality. Default algorithm used in many language libraries (often hybridised as Introsort).