Quicksort Performance
Notes
- What can we expect from Quicksort in the best case?
- It turns out, if we get a perfect split, we have Mergesort without the extra space.
- How about the worst case?
- If each time we partition, we select the pivot element as the smallest, or largest in the set remaining
- T(n) = c + T(n-1) + Θ(n)
- We should solve this recurrence.
- What if we get a bad split, but not the worst each time?
- Say a 1/10 n, 9/10 n split?
- T(n) = T(n/10) + T(9n/10) + n
- Draw the tree
-
from CLRS chapter 7.
- Note the height of this tree is O(log10/9n)
- And that there are at most n operations at each level.
- So the entire tree is bounded by O(n log10/9n)
- But is this O(n log2n)?
- Change of base loga n = logb n/logba
-
let loga n = c then ac = n Take the log base b of both sides. logb ac = logb n c logb a = logb n c = logb n / logb a or loga n = logbn / logb a
- So even in a bad split, Quicksort is O(n log2 n)
- CLRS argue the average case of quicksort to be this as well.
- They then present randomized quicksort
RANDOMIZED-PARTITION(A, p, r)
- i = Random-In-Range(p,r)
- swap(A[r], A[i])
- return PARTITION(A,p,r)