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
| Property | Merge sort | Counting sort |
|---|---|---|
| Technique | Divide and conquer: split, sort each half, merge | Count each value, turn the counts into positions and place the values, with no comparisons |
| Best case time | O(n log n) | O(n + k) |
| Average case time | O(n log n) | O(n + k) |
| Worst case time | O(n log n) | O(n + k) |
| Extra space | O(n) | O(n + k) |
| Compares values with each other | Yes | No |
| Stable (equal values keep their order) | Yes | Yes |
| In place (no extra array) | No | No |
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.
| Input | Merge sort | Counting sort |
|---|---|---|
| Mixed | 34 | 47 |
| Already sorted | 31 | 47 |
| Reversed | 29 | 47 |
| Nearly sorted | 31 | 47 |
| Duplicates | 32 | 47 |
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
- 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 Quick sort
- Merge sort vs Heap 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