Sorting
Quick sort vs Counting sort
Quick sort and Counting sort both put an array in order, in different ways. Quick sort: divide and conquer: partition around a pivot. Counting sort: count each value, turn the counts into positions and place the values, with no comparisons.
At a glance
| Property | Quick sort | Counting sort |
|---|---|---|
| Technique | Divide and conquer: partition around a pivot | 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²) | O(n + k) |
| Extra space | O(log n) | O(n + k) |
| Compares values with each other | Yes | No |
| Stable (equal values keep their order) | No | Yes |
| In place (no extra array) | Yes | No |
When to choose each
Quick sort: when to choose it
Choose quick sort when the values are not small whole numbers, when the range is wide, or when extra memory is unavailable. It compares, so it sorts anything with an order, and it partitions in place with only O(log n) of stack. Counting sort needs integral keys in a known narrow range and allocates O(n + k). Quick sort also handles duplicate-heavy data well here — the duplicates array is its cheapest input of the five.
Counting sort: when to choose it
Choose counting sort when the keys are small whole numbers and n is large. It makes no comparisons, so it is outside the O(n log n) lower bound entirely, and its cost is O(n + k) on every input — it has no worst case to protect against, where quick sort's is quadratic and arrives on sorted input under this pivot rule. It is stable, which quick sort is not. The constraints are the key type, the range, and the memory the range implies.
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 | Quick sort | Counting sort |
|---|---|---|
| Mixed | 31 | 47 |
| Already sorted | 75 | 47 |
| Reversed | 51 | 47 |
| Nearly sorted | 65 | 47 |
| Duplicates | 29 | 47 |
What the numbers show
Counting sort costs a flat 47 operations on all five arrays. Quick sort wins the two cases where its partitions are balanced — 31 against 47 on the mixed array and 29 against 47 on the duplicates — and loses the three where they are not: 47 against 75 on the already-sorted array, 47 against 65 on the nearly sorted one and 47 against 51 on the reversed one.
The sorted array is the instructive row. Quick sort needs 75 operations — 21 comparisons and 54 writes, its O(n²) worst case under a last-element pivot — to sort an array that was already in order, while counting sort needs the same 47 it needs for everything else. An algorithm that never looks at the relative order of two values cannot be ambushed by the order it is given.
Counting sort's 47 is also mostly fixed cost at this size: a count array of k + 1 = 10 cells, 9 prefix sums and three touches per value, with k larger than n. That ratio is what to check. Sorting ten thousand values in the 0–9 range, counting sort stays near n + k while quick sort needs about 1.4 n log₂ n comparisons plus its writes — tens of thousands against hundreds of thousands, and with no tail risk.
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 Counting sort
- Merge sort vs Radix sort
- Quick sort vs Heap sort
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort