Quicksort Algorithm Average Case Analysis
Quicksort Algorithm Average Case Analysis
with
Notes:
1. Replace O with θ
2. Add quiz 3c: T(N) = 27T(N/3) + θ(N2)
www.sorting-algorithms.com
Invented by C.A.R. “Tony” Hoare in 1961*
Very widely used
Somewhat complex, but fairly easy to
understand
◦ Like in basketball, it’s all
about planting a good pivot.
T(N) =
Multiply both sides by N
Rewrite, substituting N-1 for N
Subtract the equations and forget the insignificant
(in terms of big-oh) -1:
◦ NT(N) = (N+1)T(N-1) + 2N
Can we rearrange so that we can telescope?
Q11-13
NT(N) = (N+1)T(N-1) + 2N
Divide both sides by N(N+1)
Write formulas for T(N), T(N-1),T(N-2) …T(2).
Add the terms and rearrange.
Notice the familiar series
Multiply both sides by N+1.
Best, worst, average time for Quicksort
What causes the worst case?
Avoid the worst case
◦ Select pivot from the middle
◦ Randomly select pivot
◦ Median of 3 pivot selection.
◦ Median of k pivot selection
"Switch over" to a simpler sorting method
(insertion) when the subarray size gets small