Class Code: AMC621 Instructor: Huilan Chang #w13
Radix refers to the base or number system used to represent digits in radix sort
digits, each digit has $k$ **possible values, and digit $1$(在最右) is the least significant digit(最不重要). e.g. 328>299>229>199Radix sort 從LSD往MSD做sorting.由 LSD 做到 MSD, 每次只依照 “正在考慮的那個digit” 排序.例如: $d = 3, k = 10$, and each digit is between $0$ to $9$ (inclusive).
| data | | digit 1(LSD) | | digit 2 | | digit 3 |
| --- | --- | --- | --- | --- | --- | --- |
| 329 | | 720 | | 720 | | 329 |
| 457 | | 355 | | 329 | | 355 |
| 657 | | 436 | | 436 | | 436 |
| 839 | → | 457 | → | 839 | → | 457 |
| 436 | | 657 | | 355 | | 657 |
| 720 | | 329 | | 457 | | 720 |
| 355 | | 839 | | 657 | | 839 |
注意:都採用stable sorting。作用為何?例如:用digit 1和digit 2排好後,329排在355之前,再用digit 3排時,採用stable sorting,維持329排在355之前。用stable sorting才不會打破之前digits所排好的順序。
The pseudocode of radix sort is:
<aside>
RADIX–SORT($A$, $d$)
use a stable sort to sort array $A$ on digit $i$
</aside>
Try to implement RADIX-SORT!
(2) The correctness of radix sort follows by induction on the column being sorted.(skip)
(3) Analysis Suppose we use counting sort in step 2. Then there are $d$ passes in RADIX-SORT algorithm and each pass takes $\Theta(n + k)$ time. Thus RADIX–SORT takes $\Theta(d(n + k))$ time.
Note. When $d$ is a constant and $k = O(n)$, RADIX–SORT takes linear time.
<aside> ⭐
Lemma. Given $n$ $b$-bit numbers and any positive integer $r ≤ b$, RADIX-SORT correctly sorts these numbers in $\Theta((b/r)(n + 2^r))$ time.
採用:每個digit是由$r$個bits組成
</aside>
🔹 An assumption placed on the $n$ input numbers: The input is generated by a random process that distributes elements uniformly over the interval $[0, 1)$.
🔹 設計的想法: