Sorting
Selection sort vs Radix sort
Selection sort and Radix sort both put an array in order, in different ways. Selection sort: repeatedly pick the smallest remaining value. Radix sort: sort digit by digit, least significant first, with a stable counting sort for each digit.
At a glance
| Property | Selection sort | Radix sort |
|---|---|---|
| Technique | Repeatedly pick the smallest remaining value | Sort digit by digit, least significant first, with a stable counting sort for each digit |
| Best case time | O(n²) | O(d (n + k)) |
| Average case time | O(n²) | O(d (n + k)) |
| Worst case time | O(n²) | O(d (n + k)) |
| Extra space | O(1) | 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
Selection sort: when to choose it
Choose selection sort for small arrays, for values that are not fixed-width integers, and when extra memory is not available. Radix sort's structure is d passes of a stable counting sort, each with its own count and output arrays; selection sort is two nested loops and at most n − 1 swaps. Where the keys are strings with a custom collation, floating point numbers, or records compared by several fields, radix sort is not applicable at all and selection sort is.
Radix sort: when to choose it
Choose radix sort for large volumes of fixed-width keys. It is O(d(n + k)) with zero comparisons, so its cost per element does not grow with the array at all — only the number of digit positions matters — against selection sort's unconditional O(n²). It is stable by necessity, since each digit pass must preserve the order the previous pass produced. The price is memory and the requirement that keys decompose into a bounded 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 | Selection sort | Radix sort |
|---|---|---|
| Mixed | 29 | 141 |
| Already sorted | 21 | 141 |
| Reversed | 27 | 141 |
| Nearly sorted | 23 | 141 |
| Duplicates | 31 | 141 |
What the numbers show
Radix sort costs 141 operations on every one of the five arrays, all writes, and selection sort wins all five by a factor of four to six: 29 against 141 on the mixed array, 21 against 141 on the sorted one, 27 against 141 on the reversed one, 23 against 141 on the nearly sorted one and 31 against 141 on the duplicates.
The 141 decomposes exactly: the lesson always runs 3 digit passes, and each pass is a full counting sort costing 47 operations on this input. These arrays hold one-digit numbers, so two of the three passes sort by a tens and a hundreds digit that are all zero and cannot change anything. Selection sort meanwhile does its fixed 21 comparisons and between 0 and 5 swaps.
Nothing here is evidence about radix sort at a realistic size, and the shape of the two costs says why. Selection sort's work is n(n − 1)/2, so it quadruples when the array doubles. Radix sort's is d(n + k) with d fixed by the key width, so it merely doubles. For a thousand three-digit keys that is roughly 500,000 operations against roughly 3,000. The number to look at before choosing is d — and whether n is large enough to amortise d full passes.
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
- 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
- Quick sort vs Radix sort
- Heap sort vs Counting sort
- Heap sort vs Radix sort
- Counting sort vs Radix sort