```
def radix_sort( A, n, d ):
for i in 1 .. d:
stable_sort( A on digit i )
```
| 3 | 7 | 4 | 5 |
|---|---|---|---|
| 2 | 9 | 1 | 3 |
| 1 | 0 | 1 | 6 |
| 2 | 0 | 1 | 6 |
| - | 9 | 1 | 3 |
>>>
---
## Radix sort: complexity
+ **Input**: *n* items of *d* digits, each with *k* values (e.g., k=*10*)
+ e.g., using **counting** sort as the stable sort:
+ *d* iterations, each *Θ(n+k)*
+ So total complexity is *Θ(d(n+k))*
+ **Digits** need not be base *k=10* !
+ Smaller **base** *k* ⇒ more **iterations** *d*
+ Fewer **digits** *d* ⇒ each **counting** sort *Θ(n+k)* takes longer
---
## Radix: choosing digit size
+ *b*-bit items can be **split** into *r*-bit digits:
+ Then \` d = b/r \` **digits**, each with \` k = 2^r-1 \` **values**
+ e.g., *b* = 32-bit items in *r* = 8-bit digits ⇒ *d* = 4, *k* = 255
+ **Choose** r = *lg n*: then
\` Theta((b/r)(n+2^r)) \`
\` = Theta((b/(text(lg)n))(2n)) \`
\` = Theta((bn)/(text(lg)n)) \`
+ e.g., to sort *n* = \`2^16\` integers of *b* = 64-bits:
+ ⇒ Use *r* = 16-bit digits
---
## Outline for today
+ Proving all comparison sorts are *Ω(n lg n)*
+ Linear-time non-comparison sorts:
+ Counting sort
+ Radix sort and analysis
+ **Bucket sort and probabilistic proof**
+ Hash tables:
+ Collision handling by chaining
+ Hash functions and universal hashing
+ Collision handling by open addressing
---
## Bucket sort
**Assume**: values **uniformly** distributed over *[0,1)*
(For range *[a, b)*, use linear transform to *[0, 1)* )