Class Code: AMC621 Instructor: Huilan Chang #w13

Sorting by Distribution (Radix sort)

Radix refers to the base or number system used to represent digits in radix sort


例如: $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$)

  1. for $i=1$ to $d$
  2. 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>

Bucket sort 桶排序法


🔹 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)$.

🔹 設計的想法: