Sorting
Merge sort vs Quick sort
Merge sort and Quick sort both put an array in order, in different ways. Merge sort: divide and conquer: split, sort each half, merge. Quick sort: divide and conquer: partition around a pivot.
At a glance
| Property | Merge sort | Quick sort |
|---|---|---|
| Technique | Divide and conquer: split, sort each half, merge | Divide and conquer: partition around a pivot |
| Best case time | O(n log n) | O(n log n) |
| Average case time | O(n log n) | O(n log n) |
| Worst case time | O(n log n) | O(n²) |
| Extra space | O(n) | O(log n) |
| Compares values with each other | Yes | Yes |
| Stable (equal values keep their order) | Yes | No |
| In place (no extra array) | No | Yes |
When to choose each
Merge sort: when to choose it
Choose merge sort when you need the worst case bounded or the sort to be stable. It is O(n log n) in every case; quick sort is O(n log n) on average and O(n²) when the partitions go bad, and with the last element as pivot "bad" means sorted input. Merge sort is also the one that works on linked lists and on data larger than memory, because it only ever reads its inputs forwards. The cost is an O(n) buffer and more writes than quick sort on well-behaved data.
Quick sort: when to choose it
Choose quick sort when memory matters and the data is in genuinely mixed order. It partitions in place with only O(log n) of recursion stack, against merge sort's O(n) buffer, and on random data it typically does fewer comparisons and less copying than merge sort's full merge at every level. It is unstable, and its worst case is real: pick the pivot with median-of-three or at random before trusting it on input that might arrive ordered.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes).
| Input | Merge sort | Quick sort |
|---|---|---|
| Mixed | 34 | 31 |
| Already sorted | 31 | 75 |
| Reversed | 29 | 51 |
| Nearly sorted | 31 | 65 |
| Duplicates | 32 | 29 |
What the numbers show
Merge sort's totals barely move — 29, 31, 31, 32, 34 — a spread of five operations across five quite different inputs, with 20 writes in every case. Quick sort's span from 29 to 75, a factor of 2.6. That difference in variance is the comparison in one line: a guarantee against an average.
Quick sort wins where its partitions split well: 31 against 34 on the mixed array and 29 against 32 on the duplicates, with only 11 and 13 comparisons respectively. Merge sort wins the three ordered or reversed cases — 31 against 65 on the nearly sorted array and 29 against 51 on the reversed one — and on sorted input it is not close: 31 against 75. That 75 is the textbook worst case made concrete — on an ascending array the Lomuto partition peels off one element per level, 21 comparisons and 54 writes.
Note which input is each algorithm's cheapest. Quick sort's is the duplicates array at 29; merge sort's is the reversed array at 29, where each merge exhausts one half early and only 9 comparisons are needed. Neither of those is a general rule, and at scale both are O(n log n) on typical data, so the decision is made on the properties instead: stability and a hard worst-case bound, or in-place operation with a smaller constant.
See them run
More comparisons
- Bubble sort vs Selection sort
- Bubble sort vs Insertion sort
- Bubble sort vs Merge sort
- Bubble sort vs Quick sort
- Bubble sort vs Heap sort
- Bubble sort vs Counting sort
- Bubble sort vs Radix sort
- Selection sort vs Insertion sort
- Selection sort vs Merge sort
- Selection sort vs Quick sort
- Selection sort vs Heap sort
- Selection sort vs Counting sort
- Selection sort vs Radix sort
- Insertion sort vs Merge sort
- Insertion sort vs Quick sort
- Insertion sort vs Heap sort
- Insertion sort vs Counting sort
- Insertion sort vs Radix sort
- Merge sort vs Heap sort
- Merge sort vs Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Counting sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort