Sub topic 11
Radix Sort Cost and Choosing the Digit Size
This chapter assumes you have the radix_sort_lsd function from the LSD radix sort chapter. It also assumes you know Big-O notation and can split an integer into bit fields with shifts and masks. The goal is to work out how long radix sort takes. You will also see how the digit width you pick, bits_per_digit in the code, changes that cost.
The cost formula
LSD radix sort runs one stable counting sort per digit. Each counting sort on n items, with a digit that takes b possible values, costs O(n + b). That covers one pass over the input to count, one pass over the b counters for prefix sums, and one pass to place the items. With d digits, the total is:
- Time: O(d(n + b))
- Extra space: O(n + b), because the output buffer and count array are reused on every pass
For w-bit keys split into r-bit digits, b = 2^r and d = ⌈w / r⌉. Substituting gives:
O(⌈w / r⌉ · (n + 2^r))
The two terms pull against each other. A larger r means fewer passes. It also makes the count array grow exponentially.
A cost model you can run
This function counts the basic steps the formula predicts for a given digit width:
def radix_cost(n, w, r):
"""Estimated work for LSD radix sort: d passes of (n + b) steps each."""
d = -(-w // r) # ceil(w / r)
b = 1 << r # number of possible digit values
return d, b, d * (n + b)
n, w = 1_000_000, 32
for r in [1, 4, 8, 11, 16, 20, 32]:
d, b, cost = radix_cost(n, w, r)
print(f"r={r:2} passes={d:2} buckets={b:>13,} work={cost:>15,}")For one million 32-bit keys it prints these values:
| r (bits) | passes d | buckets b | work d(n + b) |
|---|---|---|---|
| 1 | 32 | 2 | 32,000,064 |
| 4 | 8 | 16 | 8,000,128 |
| 8 | 4 | 256 | 4,001,024 |
| 11 | 3 | 2,048 | 3,006,144 |
| 16 | 2 | 65,536 | 2,131,072 |
| 20 | 2 | 1,048,576 | 4,097,152 |
| 32 | 1 | 4,294,967,296 | 4,295,967,296 |
The theoretical choice: r ≈ log₂ n
Work stays low while 2^r is no bigger than about n, because then the bucket term costs no more than the item term. Picking r ≈ log₂ n gives O(w · n / log n) time. For a fixed w, that beats the O(n log n) of comparison sorts. With n = 10^6, log₂ n ≈ 20. The rounding in ⌈w / r⌉ moves the best value down to 16: 32/20 still needs two passes, so the extra buckets buy nothing.
The practical choice: keep the count array in cache
Real implementations usually use 8-bit or 11-bit digits even when the model favors 16. During the placement pass, each item is written to one of b output regions. The CPU must keep the b counters and b active write positions in cache.
- With b = 256, the counters take about 2 KB and fit easily in L1 cache. Writes go to 256 streams, which hardware handles well.
- With b = 65,536, the counters take 256 to 512 KB. Writes scatter across 65,536 regions, which causes cache and TLB misses on almost every item.
The model counts every step as equal. A cache miss can cost 100 times more than a cache hit. That is why two 16-bit passes often run slower than four 8-bit passes.
Common errors
- Choosing r from the type width instead of n. A wide digit on a small array spends most of its time clearing and scanning empty buckets.
- Ignoring d. The O(n) claim holds only when d is a constant. Keys with many bits make d grow, and radix sort then loses its advantage.
- Forgetting the O(n) buffer. Radix sort is not in-place. Comparing it with quicksort on time alone hides this memory cost.
Exercise
You sort n = 1,000 unsigned 32-bit keys. Using d(n + b), compare r = 4, 8, 11, and 16. Which r gives the least work?
Show answer
r=4: 8 × 1,016 = 8,128. r=8: 4 × 1,256 = 5,024. r=11: 3 × 3,048 = 9,144. r=16: 2 × 66,536 = 133,072. r = 8 gives the least work. With small n, the bucket term 2^r quickly dominates.
Exercise
An array holds 64-bit keys, but every value is below 2^20. How many passes does radixsortlsd make with bitsperdigit=8, and why?
Show answer
It makes 3 passes. The loop stops once max_val >> shift is 0, and a value below 2^20 has at most 20 bits. That needs ⌈20/8⌉ = 3 digits. The 64-bit type width plays no part.