1


2

 What will be the worst case for the algorithm?
 Partition is always unbalanced
 What will be the best case for the algorithm?
 Which is more likely?
 The latter, by far, except...
 Will any particular input elicit the worst case?
 Yes: Alreadysorted input

3

 In the worst case:
 T(1) = Q(1)
 T(n) = T(n  1) + Q(n)
 Works out to
 T(n) = Q(n^{2})

4

 In the best case:
 What does this work out to?

5

 Intuitively, a reallife run of quicksort will produce a mix of “bad”
and “good” splits
 Randomly distributed among the recursion tree
 Pretend for intuition that they alternate between bestcase (n/2 : n/2)
and worstcase (n1 : 1)
 What happens if we badsplit root node, then goodsplit the resulting
size (n1) node?

6

 Intuitively, a reallife run of quicksort will produce a mix of “bad”
and “good” splits
 Randomly distributed among the recursion tree
 Pretend for intuition that they alternate between bestcase (n/2 : n/2)
and worstcase (n1 : 1)
 What happens if we badsplit root node, then goodsplit the resulting
size (n1) node?
 We end up with three subarrays, size 1, (n1)/2, (n1)/2
 Combined cost of splits = n + n 1 = 2n 1 = O(n)
 No worse than if we had goodsplit the root node!

7

 Intuitively, the O(n) cost of a bad split
(or 2 or 3 bad splits) can be absorbed
into the O(n) cost of each good split
 Thus running time of alternating bad and good splits is still O(n lg n),
with slightly higher constants
 How can we be more rigorous?

8

 For simplicity, assume:
 All inputs distinct (no repeats)
 Slightly different partition() procedure
 partition around a random element, which is not included in subarrays
 all splits (0:n1, 1:n2, 2:n3, … , n1:0) equally likely
 What is the probability of a particular split happening?
 Answer: 1/n

9

 So partition generates splits
(0:n1, 1:n2, 2:n3, … , n2:1,
n1:0)
each with probability 1/n
 If T(n) is the expected running time,
 What is each term under the summation for?
 What is the Q(n)
term for?

10


11

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 Substitute it in for some value < n
 Prove that it follows for n

12

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 Substitute it in for some value < n
 Prove that it follows for n

13

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 Substitute it in for some value < n
 Prove that it follows for n

14

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 What’s the inductive hypothesis?
 Substitute it in for some value < n
 Prove that it follows for n

15

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 T(n) £ an lg n +
b for some constants a and b
 Substitute it in for some value < n
 Prove that it follows for n

16

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 T(n) £ an lg n +
b for some constants a and b
 Substitute it in for some value < n
 Prove that it follows for n

17

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 T(n) £ an lg n +
b for some constants a and b
 Substitute it in for some value < n
 The value k in the recurrence
 Prove that it follows for n

18

 We can solve this recurrence using the dreaded substitution method
 Guess the answer
 Assume that the inductive hypothesis holds
 T(n) £ an lg n +
b for some constants a and b
 Substitute it in for some value < n
 The value k in the recurrence
 Prove that it follows for n

19


20


21


22

 So T(n) £ an lg n
+ b for certain a and b
 Thus the induction holds
 Thus T(n) = O(n lg n)
 Thus quicksort runs in O(n lg n) time on average (phew!)
 Oh yeah, the summation…

23


24


25


26


27

