Sorting
Quick sort vs Radix sort
Quick sort and Radix sort both put an array in order, in different ways. Quick sort: divide and conquer: partition around a pivot. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.
At a glance
| Property | Quick sort | Radix sort |
|---|---|---|
| Technique | Divide and conquer: partition around a pivot | Sort digit by digit, least significant first, with a stable counting sort for each digit |
| Best case time | O(n log n) | O(d (n + k)) |
| Average case time | O(n log n) | O(d (n + k)) |
| Worst case time | O(n²) | O(d (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 for arrays of moderate size, for keys that are not fixed-width integers, and when memory is constrained. It sorts in place with O(log n) stack and makes no assumption about the values beyond a comparison. Radix sort requires integral keys of bounded width and runs a full stable counting sort per digit position, allocating arrays each time — on short arrays that is all setup and no payoff.
Radix sort: when to choose it
Choose radix sort for large volumes of fixed-width integer keys. Zero comparisons means the O(n log n) bound does not apply, and with d fixed its cost is linear in n where quick sort's grows as n log n. It is stable, and it has no worst case driven by input order — quick sort under a last-element pivot does, and sorted input triggers it. The costs are memory per pass and the requirement that keys split into a small number of digits.
The same inputs, counted
These numbers come from running both real implementations: operations (comparisons plus writes, a swap counting as two writes). Radix sort makes no comparisons, so its count is writes only. The lesson always runs 3 digit passes, even on these one-digit numbers.
| Input | Quick sort | Radix sort |
|---|---|---|
| Mixed | 31 | 141 |
| Already sorted | 75 | 141 |
| Reversed | 51 | 141 |
| Nearly sorted | 65 | 141 |
| Duplicates | 29 | 141 |
What the numbers show
Radix sort costs 141 operations on all five arrays, and quick sort wins all five — but look at the margins. On the mixed array it is 31 against 141 and on the duplicates 29 against 141, comfortable factors of four or five. On the reversed array it is 51 against 141; on the already-sorted array 75 against 141, and on the nearly sorted one 65 against 141: quick sort's worst case is now within a factor of two of radix sort's flat cost.
Both numbers decompose cleanly. Radix sort's 141 is 3 × 47 — three digit passes, each a complete counting sort, two of them sorting by digits that are all zero on these one-digit values. Quick sort's 75 on sorted input is 21 comparisons and 54 writes, the Lomuto partition peeling off one element per level of recursion.
The shapes diverge from there. Quick sort's average work grows as n log n and its worst case as n²; radix sort's grows as d(n + k) with d set by the key width, so doubling the array doubles the work and nothing else. For a million three-digit keys that is on the order of 28 million comparisons-plus-writes for quick sort against about 3 million operations for radix sort — and radix sort's figure does not depend on the input arriving in a convenient order.
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 Counting sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort