Skip to content
SimpleScope

Sorting

Merge sort vs Counting sort

Merge sort and Counting sort both put an array in order, in different ways. Merge sort: divide and conquer: split, sort each half, merge. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons.

At a glance

PropertyMerge sortCounting sort
TechniqueDivide and conquer: split, sort each half, mergeCount each value, turn the counts into positions and place the values, with no comparisons
Best case timeO(n log n)O(n + k)
Average case timeO(n log n)O(n + k)
Worst case timeO(n log n)O(n + k)
Extra spaceO(n)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)YesYes
In place (no extra array)NoNo

When to choose each

Merge sort: when to choose it

Choose merge sort when the keys are anything other than small whole numbers, or when the range of values is wide. It compares, so it sorts strings, floats and multi-field records, and its memory is O(n) regardless of what the values are. Counting sort's memory is O(n + k): sorting a handful of 32-bit integers would need a count array of four billion cells. Both are stable, so that is not the deciding property.

Counting sort: when to choose it

Choose counting sort when the keys are whole numbers in a range k that is small next to n. It makes no comparisons, so it is not bound by O(n log n) at all: O(n + k) beats O(n log n) once n is large and k stays small. It is stable, which is what lets radix sort use it per digit. The requirements are strict, though — integral keys, a known range, and memory proportional to that range.

The same inputs, counted

These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes). Counting sort makes no comparisons, so its count is writes only. It includes the k + 1 cells of the count array.

InputMerge sortCounting sort
Mixed3447
Already sorted3147
Reversed2947
Nearly sorted3147
Duplicates3247

What the numbers show

Merge sort wins all five cases here: 34 operations against 47 on the mixed array, 31 against 47 on the sorted one, 29 against 47 on the reversed one, 31 against 47 on the nearly sorted one and 32 against 47 on the duplicates. Both algorithms are almost indifferent to input order, so the five rows mostly restate one result.

The reason is k against n. Counting sort's 47 operations cover a count array of k + 1 = 10 cells zeroed, 7 increments, 9 prefix sums, and then a decrement, an output write and a copy-back per value — with k = 9 and n = 7 the bookkeeping is larger than the data. Merge sort needs 20 writes and 9 to 14 comparisons, and n log₂ n at n = 7 is under 20.

The inversion is a function of n/k, and it is permanent once it happens. For ten thousand values in the same 0–9 range counting sort stays at about n + k operations while merge sort grows as n log₂ n — tens of thousands against well over a hundred thousand, with the gap widening on every doubling. Counting sort also loses nothing to adversarial input, having no comparisons to mislead. Check the range before reaching for it.

See them run

More comparisons