Notes
Slide Show
Outline
1
CS 332: Algorithms
  • Quicksort
2
Review: Analyzing Quicksort
  • What will be the worst case for the algorithm?
    • Partition is always unbalanced
  • What will be the best case for the algorithm?
    • Partition is balanced
  • Which is more likely?
    • The latter, by far, except...
  • Will any particular input elicit the worst case?
    • Yes: Already-sorted input
3
Review: Analyzing Quicksort
  • In the worst case:
    • T(1) = Q(1)
    • T(n) = T(n - 1) + Q(n)
  • Works out to
  • T(n) = Q(n2)


4
Review: Analyzing Quicksort
  • In the best case:
    • T(n) = 2T(n/2) + Q(n)
  • What does this work out to?
    • T(n) = Q(n lg n)
5
Review: Analyzing Quicksort (Average Case)
  • 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
Review: Analyzing Quicksort (Average Case)
  • 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
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
  • 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
Analyzing Quicksort: Average Case
  • 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
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
  • If T(n) is the expected running time,



  • What is each term under the summation for?
  • What is the Q(n) term for?
10
Analyzing Quicksort: Average Case
  • So…







11
Analyzing Quicksort: Average Case
  • 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
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • What’s the answer?
    • Assume that the inductive hypothesis holds
    • Substitute it in for some value < n
    • Prove that it follows for n
13
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • T(n) = O(n lg n)
    • Assume that the inductive hypothesis holds
    • Substitute it in for some value < n
    • Prove that it follows for n
14
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • T(n) = O(n lg n)
    • 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
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • T(n) = O(n lg n)
    • 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
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • T(n) = O(n lg n)
    • 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
      • What value?
    • Prove that it follows for n
17
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • T(n) = O(n lg n)
    • 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
Analyzing Quicksort: Average Case
  • We can solve this recurrence using the dreaded substitution method
    • Guess the answer
      • T(n) = O(n lg n)
    • 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
      • Grind through it…
19
Analyzing Quicksort: Average Case
20
Analyzing Quicksort: Average Case
21
Analyzing Quicksort: Average Case
22
Analyzing Quicksort: Average Case
  • 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
Tightly Bounding
The Key Summation
24
Tightly Bounding
The Key Summation
25
Tightly Bounding
The Key Summation
26
Tightly Bounding
The Key Summation
27
Tightly Bounding
The Key Summation