Friday, December 20, 2013
Quicksort Recurrence
Assume array of size N with entries distinct and randomly ordered. Let CN be the expected number of compares used by quicksort. Starting with the recurrence relation:
CN=N+1+N∑k=11N(Ck−1+CN−k)C0=0=N+1+2NN∑k=1Ck−1by symmetryNCN=N(N+1)+2N∑k=1Ck−1(N−1)CN−1=(N−1)N+2N−1∑k=1Ck−1for N−1 instead of NNCN−(N−1)CN−1=2N+2CN−1subtract the last two equationsNCN=(N+1)CN−1+2Nnote it's now linear time, not quadratic!CNN+1=CN−1N+2N+1divide both sides by N(N+1)CNN+1=CN−1N+2N+1=CN−2N−1+2N+2N+1telescopeCNN+1=C12+23+⋯+2N+2N+1note C1=2CN=(N+1)(1+23+⋯+2N)+2CN∼N(1+23+⋯+2N)f(x)∼g(x) meanslim |