CS 332 Algorithms Quicksort David Luebke 1 2222021
CS 332: Algorithms Quicksort David Luebke 1 2/22/2021
Review: Analyzing Quicksort l What will be the worst case for the algorithm? n l What will be the best case for the algorithm? n l Partition is balanced Which is more likely? n l Partition is always unbalanced The latter, by far, except. . . Will any particular input elicit the worst case? n David Luebke Yes: Already-sorted input 2 2/22/2021
Review: Analyzing Quicksort l In the worst case: T(1) = (1) T(n) = T(n - 1) + (n) l Works out to T(n) = (n 2) David Luebke 3 2/22/2021
Review: Analyzing Quicksort l In the best case: T(n) = 2 T(n/2) + (n) l What does this work out to? T(n) = (n lg n) David Luebke 4 2/22/2021
Review: Analyzing Quicksort (Average Case) l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n n n David Luebke 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 goodsplit the resulting size (n-1) node? 5 2/22/2021
Review: Analyzing Quicksort (Average Case) l Intuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits n n n 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? u We end up with three subarrays, size 1, (n-1)/2 u Combined cost of splits = n + n -1 = 2 n -1 = O(n) u No worse than if we had good-split the root node! David Luebke 6 2/22/2021
Review: Analyzing Quicksort (Average Case) 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 l Thus running time of alternating bad and good splits is still O(n lg n), with slightly higher constants l How can we be more rigorous? l David Luebke 7 2/22/2021
Analyzing Quicksort: Average Case l For simplicity, assume: n n All inputs distinct (no repeats) Slightly different partition() procedure u partition around a random element, which is not included in subarrays u 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? l Answer: 1/n l David Luebke 8 2/22/2021
Analyzing Quicksort: Average Case So partition generates splits (0: n-1, 1: n-2, 2: n-3, … , n-2: 1, n-1: 0) each with probability 1/n l If T(n) is the expected running time, l What is each term under the summation for? l What is the (n) term for? l David Luebke 9 2/22/2021
Analyzing Quicksort: Average Case l So… Write it on the board David Luebke 10 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n n David Luebke Guess the answer Assume that the inductive hypothesis holds Substitute it in for some value < n Prove that it follows for n 11 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u What’s n n n David Luebke the answer? Assume that the inductive hypothesis holds Substitute it in for some value < n Prove that it follows for n 12 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) n n n David Luebke = O(n lg n) Assume that the inductive hypothesis holds Substitute it in for some value < n Prove that it follows for n 13 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) n = O(n lg n) Assume that the inductive hypothesis holds u What’s n n David Luebke the inductive hypothesis? Substitute it in for some value < n Prove that it follows for n 14 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) n Assume that the inductive hypothesis holds u T(n) n n David Luebke = O(n lg n) an lg n + b for some constants a and b Substitute it in for some value < n Prove that it follows for n 15 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) n Assume that the inductive hypothesis holds u T(n) n = O(n lg n) an lg n + b for some constants a and b Substitute it in for some value < n u What n David Luebke value? Prove that it follows for n 16 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) n Assume that the inductive hypothesis holds u T(n) n David Luebke an lg n + b for some constants a and b Substitute it in for some value < n u The n = O(n lg n) value k in the recurrence Prove that it follows for n 17 2/22/2021
Analyzing Quicksort: Average Case l We can solve this recurrence using the dreaded substitution method n Guess the answer u T(n) n Assume that the inductive hypothesis holds u T(n) n an lg n + b for some constants a and b Substitute it in for some value < n u The n = O(n lg n) value k in the recurrence Prove that it follows for n u Grind David Luebke through it… 18 2/22/2021
Analyzing Quicksort: Average Case The recurrence to be solved Plug What in inductive are we doing hypothesis here? Expand case Whatout arethe we k=0 doing here? 2 b/n is just a constant, What are we doing here? so fold it into (n) Note: leaving the same recurrence as the book David Luebke 19 2/22/2021
Analyzing Quicksort: Average Case The recurrence to be solved Distribute thewe summation What are doing here? Evaluate the summation: What are we doing here? b+b+…+b = b (n-1) Since n-1<n, 2 b(n-1)/n < 2 b What are we doing here? This summation gets its own set of slides later David Luebke 20 2/22/2021
Analyzing Quicksort: Average Case The recurrence to be solved We’llthe prove this later What hell? Distribute thewe(2 a/n) What are doingterm here? Remember, our goal is to get What are we doing here? T(n) an lg n + b David Luebke 21 Pick a large enough that How did we do this? an/4 dominates (n)+b 2/22/2021
Analyzing Quicksort: Average Case l So T(n) an lg n + b for certain a and b n n n l 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… David Luebke 22 2/22/2021
Tightly Bounding The Key Summation Split the summation for a What are we doing here? tighter bound The lg k in the second term What are we doing here? is bounded by lg n Move the lg n outside the What are we doing here? summation David Luebke 23 2/22/2021
Tightly Bounding The Key Summation The summation bound so far The lg k in the first term is What are we doing here? bounded by lg n/2 = lg n we - 1 doing here? What are Move (lg n - 1) outside the What are we doing here? summation David Luebke 24 2/22/2021
Tightly Bounding The Key Summation The summation bound so far Distribute the (lg nhere? - 1) What are we doing The summations overlap in What are we doing here? range; combine them The. What Guassian are weseries doing here? David Luebke 25 2/22/2021
Tightly Bounding The Key Summation The summation bound so far Rearrange first term, place What are we doing here? upper bound on second X Guassian What areseries we doing? Multiply it What are we doing? all out David Luebke 26 2/22/2021
Tightly Bounding The Key Summation David Luebke 27 2/22/2021
- Slides: 27