Randomized Quicksort
·running time is independent of input ordering
·no assumptions about the input distribution
·np specific input elict the worst-case behavior
·the worst-case is determined only by a random-number generator
pivot on random element
Analysis:
T(n) = r.v for running time assuming rand #‘s independent
For k = 0,1,2,..., n-1 ,let
X_k =[ 1 ,if partition genrates a k : n-k-1 split
[ 0 otherwise
X_k is an indicator random variable(指示器随机变量) only 1 or 0
E[X_k]= 0*Pr{X_k=0}+1*Pr{X_k=1}
= Pr{X_k=1}
= 1/n
each element has 1/n chance to be pivot
T(n)=[T(0)+T(n-1)+Θ(n) if 0:(n-1) split
=[T(1)+T(n-2)+Θ(n) if 1:(n-2) split
=[....
=[T(n-1)+T(0)+Θ(n) if n-1:0 split
n-1
=∑X_k(T(k)+T(n-k-1)+Θ(n))
k=0
E[T(n)]=E[∑...]
n-1
=∑X_k(T(k)+T(n-k-1)+Θ(n))
k=0
n-1
=∑E(x_k)*E[X_k(T(k)+T(n-k-1)+Θ(n))]
k=0
n-1 n-1 n-1
=1/n *∑E[T(k)] + 1/n*∑E[T(n-k-1)] + 1/n*∑Θ(n)
k=0 k=0 k=0
n-1
=2/n *∑E[T(k)] + Θ(n)
k=0
Absorb k=0,1 terms into Θ(n) for technical convenience
n-1
E[T(n))] = 2/n *∑E[T(k)] + Θ(n)
k=0
Prove E[T(n)] <= an lg(n) for const a>0
chosose a big enough so that
an lg(n) >= E[T(n)] for small n
n-1
use fact: ∑klg(k)<= 1/2* n^2 `lg(n) -1/8 * n^2
k=2
Substitution:
n-1
E[T(n)]<= 2/n* ∑ak lg(k) + Θ(n)
k=2
<= 2a/n *(1/2 * n^2 lg(n) - 1/8 * n^2) + Θ(n)
= an lg(n)-(an/4 - Θ(n))
desired - residual
<= an lg(n) if a big enough so that an/4 > Θ(n)