Skip to content
SimpleScope

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

PropertyMerge sortQuick sort
TechniqueDivide and conquer: split, sort each half, mergeDivide and conquer: partition around a pivot
Best case timeO(n log n)O(n log n)
Average case timeO(n log n)O(n log n)
Worst case timeO(n log n)O(n²)
Extra spaceO(n)O(log n)
Compares values with each otherYesYes
Stable (equal values keep their order)YesNo
In place (no extra array)NoYes

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).

InputMerge sortQuick sort
Mixed3431
Already sorted3175
Reversed2951
Nearly sorted3165
Duplicates3229

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