SlideShare a Scribd company logo
CS 332: Algorithms Quicksort
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
Review: Analyzing Quicksort In the worst case: T(1) =   (1) T(n) = T(n - 1) +   (n) Works out to T(n) =   (n 2 )
Review: Analyzing Quicksort In the best case: T(n) = 2T(n/2) +   (n) What does this work out to? T(n) =   (n lg n)
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?
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!
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?
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
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   (n) term for?
Analyzing Quicksort: Average Case So… Write it on  the board
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
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
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
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
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
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
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
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…
Analyzing Quicksort: Average Case Note: leaving the same recurrence as the book What are we doing here? The recurrence to be solved What are we doing here? What are we doing here? Plug in inductive hypothesis Expand out the k=0 case 2b/n is just a constant,  so fold it into   (n)
Analyzing Quicksort: Average Case What are we doing here? What are we doing here? Evaluate the summation:  b+b+…+b = b (n-1) The recurrence to be solved Since n-1<n, 2b(n-1)/n < 2b What are we doing here? Distribute the summation This summation gets its own set of slides later
Analyzing Quicksort: Average Case How did we do this? Pick a large enough that an/4 dominates   (n)+b  What are we doing here? Remember, our goal is to get T(n)    an  lg  n + b What the hell? We’ll prove this later What are we doing here? Distribute the (2a/n) term The recurrence to be solved
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…
Tightly Bounding  The Key Summation What are we doing here? The  lg  k in the second term is bounded by  lg  n What are we doing here? Move the  lg  n outside the summation What are we doing here? Split the summation for a tighter bound
Tightly Bounding The Key Summation The summation bound so far What are we doing here? The  lg  k in the first term is bounded by  lg  n/2 What are we doing here? lg  n/2 =  lg  n  - 1 What are we doing here? Move ( lg  n  - 1 ) outside the summation
Tightly Bounding The Key Summation The summation bound so far What are we doing here? Distribute the ( lg  n  - 1 ) What are we doing here? The summations overlap in  range; combine them What are we doing here? The Guassian series
Tightly Bounding  The Key Summation The summation bound so far What are we doing here? Rearrange first term, place upper bound on second What are we doing? X Guassian series What are we doing? Multiply it  all out
Tightly Bounding  The Key Summation

More Related Content

PPT
lecture 7
PPTX
Proofs by contraposition
PPTX
Method of direct proof
PPTX
Stochastic Processes Assignment Help
PPTX
Proof by contradiction
PDF
Skiena algorithm 2007 lecture09 linear sorting
PPTX
CMSC 56 | Lecture 5: Proofs Methods and Strategy
PPTX
CMSC 56 | Lecture 1: Propositional Logic
lecture 7
Proofs by contraposition
Method of direct proof
Stochastic Processes Assignment Help
Proof by contradiction
Skiena algorithm 2007 lecture09 linear sorting
CMSC 56 | Lecture 5: Proofs Methods and Strategy
CMSC 56 | Lecture 1: Propositional Logic

What's hot (20)

PDF
Sienna 4 divideandconquer
PPT
Discrete Math Lecture 02: First Order Logic
PPT
Retarded potential
PDF
Potentials and fields
PPTX
Mechanical Engineering Assignment Help
PDF
Differential equations final -mams
PPTX
potential and fields
PDF
Integration basics
PDF
Skiena algorithm 2007 lecture08 quicksort
PDF
annals-v181-n1-p06-p
PDF
Application of laplace transform
PDF
Ch7 angular momentum
DOCX
Persamaan schroedinger bebas waktu
PPT
Cis435 week03
PDF
Reductions
PDF
Wave functions
PDF
Probabilistic group theory, combinatorics, and computing
PDF
Foundation of KL Divergence
PDF
International Journal of Mathematics and Statistics Invention (IJMSI)
Sienna 4 divideandconquer
Discrete Math Lecture 02: First Order Logic
Retarded potential
Potentials and fields
Mechanical Engineering Assignment Help
Differential equations final -mams
potential and fields
Integration basics
Skiena algorithm 2007 lecture08 quicksort
annals-v181-n1-p06-p
Application of laplace transform
Ch7 angular momentum
Persamaan schroedinger bebas waktu
Cis435 week03
Reductions
Wave functions
Probabilistic group theory, combinatorics, and computing
Foundation of KL Divergence
International Journal of Mathematics and Statistics Invention (IJMSI)
Ad

Viewers also liked (13)

PPT
Cis435 week02
PDF
cvjeroendewandel
PPT
Predicate &amp; quantifier
PDF
Solving recurrences
PDF
Recurrence relation
PDF
Recurrence relations
PPTX
Recurrence relationclass 5
PPTX
recurrence relations
PPT
Reccurrence relations
PDF
Recurrences
PPTX
Predicates and quantifiers
PPT
Merge sort
PPTX
Algorithm analysis (All in one)
Cis435 week02
cvjeroendewandel
Predicate &amp; quantifier
Solving recurrences
Recurrence relation
Recurrence relations
Recurrence relationclass 5
recurrence relations
Reccurrence relations
Recurrences
Predicates and quantifiers
Merge sort
Algorithm analysis (All in one)
Ad

Similar to lecture 8 (20)

PPT
Average case Analysis of Quicksort
PPT
lecture7.ppt
PPT
lecture8.ppt
PPTX
P, NP and NP-Complete, Theory of NP-Completeness V2
PPT
recurrence relation is explained in this
PPT
Approx
PDF
Unit-1 DAA_Notes.pdf
PDF
6_12_13Asymptotic analysiskfnhlkjsbfbkjs.pdf
PPTX
probability assignment help (2)
PPTX
Analysis of algorithms
PPTX
T2311 - Ch 4_Part1.pptx
PPTX
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
PDF
Asymptotic notation
PPT
Divide and conquer
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPT
Time complexity
PPTX
RECURRENCE EQUATIONS & ANALYZING THEM
PPT
Analysis Of Algorithms Ii
PDF
Average case Analysis of Quicksort
lecture7.ppt
lecture8.ppt
P, NP and NP-Complete, Theory of NP-Completeness V2
recurrence relation is explained in this
Approx
Unit-1 DAA_Notes.pdf
6_12_13Asymptotic analysiskfnhlkjsbfbkjs.pdf
probability assignment help (2)
Analysis of algorithms
T2311 - Ch 4_Part1.pptx
PCC_CS_404_OUCHITYAPRODHAN_17000124029[1].pptx
Asymptotic notation
Divide and conquer
3. Recursion and Recurrences.ppt detail about recursive learning
Time complexity
RECURRENCE EQUATIONS & ANALYZING THEM
Analysis Of Algorithms Ii

More from sajinsc (20)

PPT
lecture 30
PPT
lecture 29
PPT
lecture 28
PPT
lecture 27
PPT
lecture 26
PPT
lecture 25
PPT
lecture 24
PPT
lecture 23
PPT
lecture 22
PPT
lecture 21
PPT
lecture 20
PPT
lecture 19
PPT
lecture 18
PPT
lecture 17
PPT
lecture 16
PPT
lecture 15
PPT
lecture 14
PPT
lecture 13
PPT
lecture 12
PPT
lecture 11
lecture 30
lecture 29
lecture 28
lecture 27
lecture 26
lecture 25
lecture 24
lecture 23
lecture 22
lecture 21
lecture 20
lecture 19
lecture 18
lecture 17
lecture 16
lecture 15
lecture 14
lecture 13
lecture 12
lecture 11

Recently uploaded (20)

PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Classroom Observation Tools for Teachers
PPTX
GDM (1) (1).pptx small presentation for students
PDF
RMMM.pdf make it easy to upload and study
PDF
Pre independence Education in Inndia.pdf
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Institutional Correction lecture only . . .
PDF
Complications of Minimal Access Surgery at WLH
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Lesson notes of climatology university.
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Sports Quiz easy sports quiz sports quiz
Renaissance Architecture: A Journey from Faith to Humanism
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Insiders guide to clinical Medicine.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Classroom Observation Tools for Teachers
GDM (1) (1).pptx small presentation for students
RMMM.pdf make it easy to upload and study
Pre independence Education in Inndia.pdf
Anesthesia in Laparoscopic Surgery in India
Institutional Correction lecture only . . .
Complications of Minimal Access Surgery at WLH
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
STATICS OF THE RIGID BODIES Hibbelers.pdf
Pharma ospi slides which help in ospi learning
Lesson notes of climatology university.
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.

lecture 8

  • 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) =  (1) T(n) = T(n - 1) +  (n) Works out to T(n) =  (n 2 )
  • 4. Review: Analyzing Quicksort In the best case: T(n) = 2T(n/2) +  (n) What does this work out to? T(n) =  (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  (n) term for?
  • 10. Analyzing Quicksort: Average Case So… Write it on the board
  • 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 Note: leaving the same recurrence as the book What are we doing here? The recurrence to be solved What are we doing here? What are we doing here? Plug in inductive hypothesis Expand out the k=0 case 2b/n is just a constant, so fold it into  (n)
  • 20. Analyzing Quicksort: Average Case What are we doing here? What are we doing here? Evaluate the summation: b+b+…+b = b (n-1) The recurrence to be solved Since n-1<n, 2b(n-1)/n < 2b What are we doing here? Distribute the summation This summation gets its own set of slides later
  • 21. Analyzing Quicksort: Average Case How did we do this? Pick a large enough that an/4 dominates  (n)+b What are we doing here? Remember, our goal is to get T(n)  an lg n + b What the hell? We’ll prove this later What are we doing here? Distribute the (2a/n) term The recurrence to be solved
  • 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 What are we doing here? The lg k in the second term is bounded by lg n What are we doing here? Move the lg n outside the summation What are we doing here? Split the summation for a tighter bound
  • 24. Tightly Bounding The Key Summation The summation bound so far What are we doing here? The lg k in the first term is bounded by lg n/2 What are we doing here? lg n/2 = lg n - 1 What are we doing here? Move ( lg n - 1 ) outside the summation
  • 25. Tightly Bounding The Key Summation The summation bound so far What are we doing here? Distribute the ( lg n - 1 ) What are we doing here? The summations overlap in range; combine them What are we doing here? The Guassian series
  • 26. Tightly Bounding The Key Summation The summation bound so far What are we doing here? Rearrange first term, place upper bound on second What are we doing? X Guassian series What are we doing? Multiply it all out
  • 27. Tightly Bounding The Key Summation