Sort integer or string keys by processing one digit, byte, or character position at a time.
Radix sort sorts keys by decomposing them into smaller parts such as digits, bytes, or characters. Instead of comparing whole keys directly, it groups keys by one position at a time.
It is most useful when keys have fixed width or bounded length, such as integers, fixed length strings, byte arrays, or identifiers.
Problem
Given an array of keys, sort the keys by their full value using their digit representation.
For base , each key can be written as:
where each digit satisfies:
Main Variants
Radix sort has two common directions.
| variant | direction | typical use |
|---|---|---|
| LSD radix sort | least significant digit to most significant digit | fixed width integers and strings |
| MSD radix sort | most significant digit to least significant digit | strings, variable length keys, lexicographic sorting |
LSD radix sort relies on a stable subroutine, usually stable counting sort. MSD radix sort recursively partitions by leading digits.
LSD Algorithm
radix_sort_lsd(A, digits, base):
for p from 0 to digits - 1:
stable_counting_sort_by_digit(A, p, base)
return AHere, position means the least significant digit.
Example
Sort:
Using decimal digits.
After sorting by ones digit:
After sorting by tens digit:
After sorting by hundreds digit:
The final array is sorted.
Correctness
For LSD radix sort, the stable pass on digit sorts the array by the lowest digits. This holds by induction. The base case follows from sorting by the least significant digit. The induction step works because the stable sort orders by the new digit while preserving the previous order among equal new digits.
After all digit positions have been processed, the array is sorted by every digit, so it is sorted by the full key.
Complexity
Let:
- be the number of keys
- be the number of digit positions
- be the base
Each stable counting pass costs:
Total time:
Space:
For fixed width machine integers with constant and practical base , this is linear in .
When to Use
Use radix sort when:
- keys are integers, bytes, strings, or fixed width records
- digit extraction is cheap
- the alphabet or digit range is bounded
- stable sorting by individual positions is available
Avoid radix sort when keys are large objects with expensive digit extraction, highly variable encodings, or no natural bounded digit representation.
Implementation
def radix_sort(a):
if not a:
return a
max_value = max(a)
exp = 1
base = 10
while max_value // exp > 0:
a = counting_sort_by_digit(a, exp, base)
exp *= base
return a
def counting_sort_by_digit(a, exp, base):
count = [0] * base
for x in a:
digit = (x // exp) % base
count[digit] += 1
for i in range(1, base):
count[i] += count[i - 1]
out = [0] * len(a)
for i in range(len(a) - 1, -1, -1):
x = a[i]
digit = (x // exp) % base
count[digit] -= 1
out[count[digit]] = x
return out