Course: 113-1 Introduction to Algorithms. Lecturer: Huilan Chang. 2024.11.08

Sorting by Distribution (Radix sort)


例如: $d = 3, k = 10$, and each digit is between $0$ to $9$ (inclusive).

| data |  | digit 1      |  | 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所排好的順序。

<aside>
⭐

The pseudocode of radix sort is:
RADIX–SORT($A$, $d$)

1. **for** $i ← 1$ **to** $d$ **do**
2.     use a stable sort to sort array $A$ on digit $i$.
</aside>

(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. Note. 每個digit是由$r$個bits組成

</aside>

Bucket sort 桶子排序法


Screenshot 2024-11-12 at 15.31.01.png

We now analyze the average-case running time of bucket sort, by computing the expected value of the running time, where we take the expectation over the input distribution. Taking expectations of both sides and using linearity of expectation, we have

20A8985B-CC57-45B7-B9B1-E7A7E73A33C2_4_5005_c.jpeg

It is no surprise that each bucket $i$ has the same value of $E [n_i^2]$, since each value in the input array $A$ is equally likely to fall in any bucket. Define indicator random variables $X_{ij}=I\{A[j] \text{ falls in bucket } i\}$ for $i=0,1,\ldots, n-1$ and $j=1,2,\ldots, n$. (接續板書)

<aside>

</aside>