|
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: Already-sorted input
|
|
3
|
- In the worst case:
- T(1) = Q(1)
- T(n) = T(n - 1) + Q(n)
- Works out to
- T(n) = Q(n2)
|
|
4
|
- In the best case:
- What does this work out to?
|
|
5
|
- Intuitively, a real-life 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 best-case (n/2 : n/2)
and worst-case (n-1 : 1)
- What happens if we bad-split root node, then good-split the resulting
size (n-1) node?
|
|
6
|
- Intuitively, a real-life 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 best-case (n/2 : n/2)
and worst-case (n-1 : 1)
- What happens if we bad-split root node, then good-split the resulting
size (n-1) node?
- We end up with three subarrays, size 1, (n-1)/2, (n-1)/2
- Combined cost of splits = n + n -1 = 2n -1 = O(n)
- No worse than if we had good-split 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:n-1, 1:n-2, 2:n-3, … , n-1:0) equally likely
- What is the probability of a particular split happening?
- Answer: 1/n
|
|
9
|
- So partition generates splits
(0:n-1, 1:n-2, 2:n-3, … , n-2:1,
n-1: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
|
|