《算法导论》第三版第8章 线性时间排序 练习&思考题 个人答案

该博客详细解答了《算法导论》第三版第8章关于线性时间排序的练习题,涵盖了排序算法的下界、计数排序、基数排序和桶排序等内容,并探讨了线性时间原址排序、变长数据项排序的策略。博主通过解题过程展示了各种排序算法的原理和应用,包括复杂度分析和稳定性讨论,以及针对不同情况的排序算法选择。
摘要由CSDN通过智能技术生成

8.1 排序算法的下界

8.1-1

解:n-1。

8.1-2

解: Θ ( n lg ⁡ n ) \Theta(n\lg n) Θ(nlgn)

8.1-3

证明:
n ! 2 ≤ 2 n \frac{n!}{2} \le 2^n 2n!2n
n ! n ≤ 2 n \frac{n!}{n} \le 2^n nn!2n
n ! 2 n ≤ 2 n ⇔ n ! ≤ 4 n \frac{n!}{2^n} \le 2^n \Leftrightarrow n! \le 4^n 2nn!2nn!4n
对后两种,只能对较小的n存在。

8.1-4

证明:
( k ! ) n / k ≤ 2 h (k!)^{n/k} \le 2^h (k!)n/k2h
h ≥ lg ⁡ ( k ! ) n / k = ( n / k ) lg ⁡ ( k ! ) ≥ ( n / k ) ( k / 2 ) lg ⁡ ( k / 2 ) (ex 8.1.2) = 1 2 n lg ⁡ k − 1 2 n = Ω ( n lg ⁡ k ) \begin{aligned} h &\ge \lg(k!)^{n/k} \\ &= (n/k)\lg(k!) \\ &\ge (n/k)(k/2)\lg(k/2) & \text{(ex 8.1.2)}\\ &= \frac{1}{2}n\lg{k} - \frac{1}{2}n \\ &= \Omega(n\lg{k}) \end{aligned} hlg(k!)n/k=(n/k)lg(k!)(n/k)(k/2)lg(k/2)=21nlgk21n=Ω(nlgk)(ex 8.1.2)

8.2 计数排序

8.2-1

解:中间过程略,最后B是{0, 0, 1, 1, 2, 2, 3, 3, 4, 6, 6},C是{0, 2, 4, 6, 8, 8, 9}

8.2-2 8.2-3

证明(来自参考答案):
Notice that the correctness argument in the text does not depend on the order in which A A A is processed. The algorithm is correct no matter what order is used!
But the modified algorithm is not stable. As before, in the final for loop an element equal to one taken from A A A earlier is placed before the earlier one (i.e., at a lower index position) in the output array B B B. The original algorithm was stable because an element taken from A A A later started out with a lower index than one taken earlier. But in the modified algorithm, an element taken from A A A later started out with a higher index than one taken earlier.
In particular, the algorithm still places the elements with value k k k in positions C [ k − 1 ] + 1 C[k - 1] + 1 C[k1]+1 through C [ k ] C[k] C[k], but in the reverse order of their appearance in A A A.

8.2-4

解:

COUNTING-INTERVAL(A, a, b)
let C[0..k] be a new array
count = 0
for i = 0 to k
    C[i] = 0
for j = 1 to A.length
    C[A[j]] = C[A[j]] + 1
for i = ceiling(a) to floor(b)
    count = count + C[i]
return count

8.3 基数排序

8.3-1

解:
0 1 2 3 COW SE A T A B B AR DOG TE A B A R B IG SEA MO B E A R B OX RUG TA B T A R C OW ROW DO G S E A D IG MOB RU G T E A D OG BOX DI G D I G E AR TAB BI G B I G F OX BAR BA R M O B M OB EAR EA R D O G N OW TAR TA R C O W R OW DIG CO W R O W R UG BIG RO W N O W S EA TEA NO W B O X T AB NOW BO X F O X T AR FOX FO X R U G T EA \begin{array}{cccc} 0 & 1 & 2 & 3 \\ \hline \text{COW} & \text{SE$\textbf{A}$} & \text{T$\textbf{A}$B} & \text{$\textbf{B}$AR} \\ \text{DOG} & \text{TE$\textbf{A}$} & \text{B$\textbf{A}$R} & \text{$\textbf{B}$IG} \\ \text{SEA} & \text{MO$\textbf{B}$} & \text{E$\textbf{A}$R} & \text{$\textbf{B}$OX} \\ \text{RUG} & \text{TA$\textbf{B}$} & \text{T$\textbf{A}$R} & \text{$\textbf{C}$OW} \\ \text{ROW} & \text{DO$\textbf{G}$} & \text{S$\textbf{E}$A} & \text{$\textbf{D}$IG} \\ \text{MOB} & \text{RU$\textbf{G}$} & \text{T$\textbf{E}$A} & \text{$\textbf{D}$OG} \\ \text{BOX} & \text{DI$\textbf{G}$} & \text{D$\textbf{I}$G} & \text{$\textbf{E}$AR} \\ \text{TAB} & \text{BI$\textbf{G}$} & \text{B$\textbf{I}$G} & \text{$\textbf{F}$OX} \\ \text{BAR} & \text{BA$\textbf{R}$} & \text{M$\textbf{O}$B} & \text{$\textbf{M}$OB} \\ \text{EAR} & \text{EA$\textbf{R}$} & \text{D$\textbf{O}$G} & \text{$\textbf{N}$OW} \\ \text{TAR} & \text{TA$\textbf{R}$} & \text{C$\textbf{O}$W} & \text{$\textbf{R}$OW} \\ \text{DIG} & \text{CO$\textbf{W}$} & \text{R$\textbf{O}$W} & \text{$\textbf{R}$UG} \\ \text{BIG} & \text{RO$\textbf{W}$} & \text{N$\textbf{O}$W} & \text{$\textbf{S}$EA} \\ \text{TEA} & \text{NO$\textbf{W}$} & \text{B$\textbf{O}$X} & \text{$\textbf{T}$AB} \\ \text{NOW} & \text{BO$\textbf{X}$} & \text{F$\textbf{O}$X} & \text{$\textbf{T}$AR} \\ \text{FOX} & \text{FO$\textbf{X}$} & \text{R$\textbf{U}$G} & \text{$\textbf{T}$EA} \\ \end{array} 0COWDOGSEARUGROWMOBBOXTABBAREARTARDIGBIGTEANOWFOX1SEATEAMOBTABDOGRUGDIGBIGBAREARTARCOWROWNOWBOXFOX2TABBAREARTARSEATEADIGBIGMOBDOGCOWROWNOWBOXFOXRUG3BARBIGBOXCOWDIGDOGEARFOXMOBNOWROWRUGSEATABTARTEA

8.3-2

插入排序和归并排序是稳定的。对相等的元素记录其原下标,额外开销 O ( n lg ⁡ n ) O(n\lg n) O(nlgn)时间和O(n)空间。

8.3-3

证明(来自参考答案):
Basis: If d = 1 d = 1 d=1, there’s only one digit, so sorting on that digit sorts the array.
Inductive step: Assuming that radix sort works for d − 1 d - 1 d1 digits, we’ll show that it works for d d d digits.
Radix sort sorts separately on each digit, starting from digit 1 1 1. Thus, radix sort of d d d digits, which sorts on digits 1 , … , d 1, \ldots, d 1,,d is equivalent to radix sort of the low-order d − 1 d - 1 d1 digits followed by a sort on digit d d d. By our induction hypothesis, the sort of the low-order d − 1 d - 1 d1 digits works, so just before the sort on digit d d d, the elements are in order according to their low-order d − 1 d - 1 d1 digits.
The sort on digit d d d will order the elements by their d d dth digit. Consider two elements, a a a and b b b, with dth digits a d a_d ad and b d b_d bd respectively.

If a d < b d a_d < b_d ad<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值