快速排序平均时间复杂度分析

x 1 , … , x n x_1,\dots,x_n x1,,xn进行快速排序
partition node: x [ k ] x[k] x[k]为随机划分元
x [ k ] x[k] x[k] 被选择的概率为 1 / n 1/n 1/n,令 C n C_n Cn 为规模为n的问题的比较次数(即 T ( n ) T(n) T(n) )
C n = ( n − 1 ) + 1 n ∑ k = 1 n ( C k − 1 + C n − k ) = ( n − 1 ) + 1 n ∑ k = 1 n C k − 1 + 1 n ∑ k = 1 n C n − k = ( n − 1 ) + 1 n ∑ k = 0 n − 1 C k + 1 n ∑ k = 0 n − 1 C k = ( n − 1 ) + 2 n ∑ k = 0 n − 1 C k \begin{align*} C_n&=(n-1)+\frac{1}{n}\sum_{k=1}^n(C_{k-1}+C_{n-k})\\ &=(n-1)+\frac{1}{n}\sum_{k=1}^n C_{k-1}+\frac{1}{n}\sum_{k=1}^n C_{n-k}\\ &=(n-1)+\frac{1}{n}\sum_{k=0}^{n-1} C_{k}+\frac{1}{n}\sum_{k=0}^{n-1} C_{k}\\ &=(n-1)+\frac{2}{n}\sum_{k=0}^{n-1} C_{k} \end{align*} Cn=(n1)+n1k=1n(Ck1+Cnk)=(n1)+n1k=1nCk1+n1k=1nCnk=(n1)+n1k=0n1Ck+n1k=0n1Ck=(n1)+n2k=0n1Ck
因此
n C n = n ( n − 1 ) + 2 ∑ k = 0 n − 1 C k ① nC_n=n(n-1)+2\sum_{k=0}^{n-1}C_k\quad ① nCn=n(n1)+2k=0n1Ck
n − 1 n-1 n1 代入 n n n,得
( n − 1 ) C n − 1 = ( n − 1 ) ( n − 2 ) + 2 ∑ k = 0 n − 2 C k ② (n-1)C_{n-1}=(n-1)(n-2)+2\sum_{k=0}^{n-2}C_k\quad② (n1)Cn1=(n1)(n2)+2k=0n2Ck
① − ② ①-②
n C n − ( n − 1 ) C n − 1 = 2 ( n − 1 ) + 2 C n − 1 ⇒ n C n = 2 ( n − 1 ) + ( n + 1 ) C n − 1 ③ ⇒ C n n + 1 = C n − 1 n + 2 ( n − 1 ) n ( n + 1 ) \begin{align*} &nC_n-(n-1)C_{n-1}=2(n-1)+2C_{n-1}\\ \Rightarrow &nC_n=2(n-1)+(n+1)C_{n-1}\quad ③\\ \Rightarrow &\frac{C_n}{n+1}=\frac{C_{n-1}}{n}+\frac{2(n-1)}{n(n+1)} \end{align*} nCn(n1)Cn1=2(n1)+2Cn1nCn=2(n1)+(n+1)Cn1n+1Cn=nCn1+n(n+1)2(n1)
D n = C n n + 1 D_n=\frac{C_n}{n+1} Dn=n+1Cn
D n = D n − 1 + 2 ( n − 1 ) n ( n + 1 ) ④ D_n=D_{n-1}+\frac{2(n-1)}{n(n+1)}\quad④ Dn=Dn1+n(n+1)2(n1)
因此
D n = 2 ∑ j = 1 n j − 1 j ( j + 1 ) = 2 ∑ j = 1 n 2 j + 1 − 2 ∑ j = 1 n 1 j = 4 ∑ j = 2 n + 1 1 j − 2 ∑ j = 1 n 1 j = 2 ∑ j = 1 n 1 j + 4 n + 1 − 4 = 2 H n − 4 n n + 1 ≈ 2 ln ⁡ n + O ( 1 ) = 2 log ⁡ 2 e log ⁡ 2 n + O ( 1 ) ≈ 1.44 log ⁡ 2 n \begin{align*} D_n&=2\sum_{j=1}^n\frac{j-1}{j(j+1)}\\ &=2\sum_{j=1}^n\frac{2}{j+1}-2\sum_{j=1}^n\frac{1}{j}\\ &=4\sum_{j=2}^{n+1}\frac{1}{j}-2\sum_{j=1}^n\frac{1}{j}\\ &=2\sum_{j=1}^n\frac{1}{j}+\frac{4}{n+1}-4\\ &=2H_n-\frac{4n}{n+1}\\ &\approx2\ln n+O(1)\\ &=\frac{2}{\log_2 e}\log_2 n+O(1)\\ &\approx1.44\log_2 n \end{align*} Dn=2j=1nj(j+1)j1=2j=1nj+122j=1nj1=4j=2n+1j12j=1nj1=2j=1nj1+n+144=2Hnn+14n2lnn+O(1)=log2e2log2n+O(1)1.44log2n
因此
C n ≈ 1.44 n log ⁡ 2 n = O ( n log ⁡ n ) C_n\approx1.44n\log_2 n=O(n\log n) Cn1.44nlog2n=O(nlogn)

你可能感兴趣的:(算法,排序算法)