CS 33: Aorithms Quicsort David Luebe //03
Homewor Assiged today, due ext Wedesday Will be o web page shortly after class Go over ow David Luebe //03
Review: Quicsort Sorts i place Sorts O( ) i the average case Sorts O( ) i the worst case But i practice, it s quic Ad the worst case does t happe ofte (but more o this later ) David Luebe 3 //03
Quicsort Aother divide-ad-coquer aorithm The array A[p..r] is partitioed ito two oempty subarrays A[p..q] ad A[q+..r] Ivariat: All elemets i A[p..q] are less tha all elemets i A[q+..r] The subarrays are recursively sorted by calls to quicsort Ulie merge sort, o combiig step: two subarrays form a already-sorted array David Luebe 4 //03
Quicsort Code Quicsort(A, p, r) { if (p < r) { q = Partitio(A, p, r); Quicsort(A, p, q); Quicsort(A, q+, r); } } David Luebe 5 //03
Partitio Clearly, all the actio taes place i the partitio() fuctio Rearrages the subarray i place Ed result: Two subarrays All values i first subarray all values i secod Returs the idex of the pivot elemet separatig the two subarrays How do you suppose we implemet this? David Luebe 6 //03
Partitio I Words Partitio(A, p, r): Select a elemet to act as the pivot (which?) Grow two regios, A[p..i] ad A[j..r] All elemets i A[p..i] <= pivot All elemets i A[j..r] >= pivot Icremet i util A[i] >= pivot Decremet j util A[j] <= pivot Swap A[i] ad A[j] Repeat util i >= j Retur j Note: slightly differet from boo s partitio() David Luebe 7 //03
Partitio Code Partitio(A, p, r) x = A[p]; i = p - ; j = r + ; while (TRUE) repeat j--; util A[j] <= x; repeat i++; util A[i] >= x; if (i < j) Swap(A, i, j); else retur j; Illustrate o A = {5, 3,, 6, 4,, 3, 7}; What is the ruig time of partitio()? David Luebe 8 //03
Partitio Code Partitio(A, p, r) x = A[p]; i = p - ; j = r + ; while (TRUE) repeat j--; util A[j] <= x; repeat i++; util A[i] >= x; if (i < j) Swap(A, i, j); else retur j; partitio() rus i O() time David Luebe 9 //03
Aalyzig Quicsort What will be the worst case for the aorithm? Partitio is always ubalaced What will be the best case for the aorithm? Partitio is perfectly balaced Which is more liely? The latter, by far, except... Will ay particular iput elicit the worst case? Yes: Already-sorted iput David Luebe 0 //03
Aalyzig Quicsort I the worst case: T() = () T() = T( - ) + () Wors out to T() = ( ) David Luebe //03
Aalyzig Quicsort I the best case: T() = T(/) + () What does this wor out to? T() = ( ) David Luebe //03
Improvig Quicsort The real liability of quicsort is that it rus i O( ) o already-sorted iput Boo discusses two solutios: Radomize the iput array, OR Pic a radom pivot elemet How will these solve the problem? By isurig that o particular iput ca be chose to mae quicsort ru i O( ) time David Luebe 3 //03
Aalyzig Quicsort: Average Case Assumig radom iput, average-case ruig time is much closer to O( ) tha O( ) First, a more ituitive explaatio/example: Suppose that partitio() always produces a 9-to- split. This loos quite ubalaced! The recurrece is thus: T() = T(9/0) + T(/0) + Use istead of O() for coveiece (how?) How deep will the recursio go? (draw it) David Luebe 4 //03
Aalyzig Quicsort: Average Case Ituitively, a real-life ru of quicsort will produce a mix of bad ad good splits Radomly distributed amog the recursio tree Preted for ituitio that they alterate betwee best-case (/ : /) ad worst-case (- : ) What happes if we bad-split root ode, the good-split the resultig size (-) ode? David Luebe 5 //03
Aalyzig Quicsort: Average Case Ituitively, a real-life ru of quicsort will produce a mix of bad ad good splits Radomly distributed amog the recursio tree Preted for ituitio that they alterate betwee best-case (/ : /) ad worst-case (- : ) What happes if we bad-split root ode, the good-split the resultig size (-) ode? We fail Eglish David Luebe 6 //03
Aalyzig Quicsort: Average Case Ituitively, a real-life ru of quicsort will produce a mix of bad ad good splits Radomly distributed amog the recursio tree Preted for ituitio that they alterate betwee best-case (/ : /) ad worst-case (- : ) What happes if we bad-split root ode, the good-split the resultig size (-) ode? We ed up with three subarrays, size, (-)/, (-)/ Combied cost of splits = + - = - = O() No worse tha if we had good-split the root ode! David Luebe 7 //03
Aalyzig Quicsort: Average Case Ituitively, the O() cost of a bad split (or or 3 bad splits) ca be absorbed ito the O() cost of each good split Thus ruig time of alteratig bad ad good splits is still O( ), with slightly higher costats How ca we be more rigorous? David Luebe 8 //03
Aalyzig Quicsort: Average Case For simplicity, assume: All iputs distict (o repeats) Slightly differet partitio() procedure partitio aroud a radom elemet, which is ot icluded i subarrays all splits (0:-, :-, :-3,, -:0) equally liely What is the probability of a particular split happeig? Aswer: / David Luebe 9 //03
Aalyzig Quicsort: Average Case So partitio geerates splits (0:-, :-, :-3,, -:, -:0) each with probability / If T() is the expected ruig time, T 0 T T What is each term uder the summatio for? What is the () term for? David Luebe 0 //03
Aalyzig Quicsort: Average Case So T 0 T T 0 Note: this is just lie the boo s recurrece (p66), except that the summatio starts with =0 We ll tae care of that i a secod T Write it o the board David Luebe //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer Assume that the iductive hypothesis holds Substitute it i for some value < Prove that it follows for David Luebe //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer What s the aswer? Assume that the iductive hypothesis holds Substitute it i for some value < Prove that it follows for David Luebe 3 //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds Substitute it i for some value < Prove that it follows for David Luebe 4 //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds What s the iductive hypothesis? Substitute it i for some value < Prove that it follows for David Luebe 5 //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < Prove that it follows for David Luebe 6 //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < What value? Prove that it follows for David Luebe 7 //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < The value i the recurrece Prove that it follows for David Luebe 8 //03
Aalyzig Quicsort: Average Case We ca solve this recurrece usig the dreaded substitutio method Guess the aswer T() = O( ) Assume that the iductive hypothesis holds T() a + b for some costats a ad b Substitute it i for some value < The value i the recurrece Prove that it follows for Grid through it David Luebe 9 //03
T Aalyzig Quicsort: Average Case T 0 0 b a b b a b a b a b The recurrece to be solved Plug What i iductive are we doig hypothesis here? Expad What out are the we =0 doig case here? b/ is just a costat, What are we doig here? so fold it ito () Note: leavig the same recurrece as the boo David Luebe 30 //03
Aalyzig Quicsort: Average Case T a b a a a b b b ( ) This summatio gets its ow set of slides later The recurrece to be solved Distribute What are the we summatio doig here? Evaluate the summatio: What are we doig here? b+b+ +b = b (-) Sice What -<, are we b(-)/ doig here? < b David Luebe 3 //03
Aalyzig Quicsort: Average Case T a a a a a a 4 b b b 8 b b b The recurrece to be solved What We ll the prove hell? this later Distribute What are the we (a/) doig term here? Remember, our goal is to get What are we doig here? T() a + b Pic a large eough that How did we do this? a/4 domiates ()+b David Luebe 3 //03 a 4
Aalyzig Quicsort: Average Case So T() a + b for certai a ad b Thus the iductio holds Thus T() = O( ) Thus quicsort rus i O( ) time o average (phew!) Oh yeah, the summatio David Luebe 33 //03
What are we doig here? The i the secod term is bouded by Tightly Boudig The Key Summatio What are we doig here? Split the summatio for a tighter boud David Luebe 34 //03 is bouded by What are we doig here? Move the outside the summatio
The summatio boud so far Tightly Boudig The Key Summatio What are we doig here? The i the first term is bouded by / David Luebe 35 //03 bouded by / What are we doig here? / = - What are we doig here? Move ( - ) outside the summatio
The summatio boud so far Tightly Boudig The Key Summatio What are we doig here? Distribute the ( - ) David Luebe 36 //03 ) ( What are we doig here? The summatios overlap i rage; combie them What are we doig here? The Guassia series
The summatio boud so far Tightly Boudig The Key Summatio ) ( What are we doig here? Rearrage first term, place upper boud o secod David Luebe 37 //03 4 8 upper boud o secod What are we doig? X Guassia series What are we doig? Multiply it all out
Tightly Boudig The Key Summatio 8 8 whe 4 Doe!!! David Luebe 38 //03