Skip to content
SimpleScope

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

PropertySelection sortRadix sort
TechniqueRepeatedly pick the smallest remaining valueSort digit by digit, least significant first, with a stable counting sort for each digit
Best case timeO(n²)O(d (n + k))
Average case timeO(n²)O(d (n + k))
Worst case timeO(n²)O(d (n + k))
Extra spaceO(1)O(n + k)
Compares values with each otherYesNo
Stable (equal values keep their order)NoYes
In place (no extra array)YesNo

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.

InputSelection sortRadix sort
Mixed29141
Already sorted21141
Reversed27141
Nearly sorted23141
Duplicates31141

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