CSE 326: Data Structures Qucksort Comparson Sortng Bound Steve Setz Wnter 2009 Qucksort Qucksort uses a dvde and conquer strategy, but does not requre the O(N) extra space that MergeSort does. Here s the dea for sortng array S: 1. Pck an element v n S. Ths s the pvot value. 2. Partton S-{v nto two dsont subsets, S 1 and S 2 such that: elements n S 1 are all v elements n S 2 are all v 3. Return concatenaton of QuckSort(S 1 ), v, QuckSort(S 2 ) Recurson ends when Qucksort( ) receves an array of length 0 or 1. 2 The steps of Qucksort Qucksort Example S 13 13 81 92 43 65 31 57 0 43 31 65 26 57 26 92 75 75 0 81 select pvot value S 1 S 2 partton S S 1 S 2 0 13 26 31 43 57 65 75 81 92 S 0 13 26 31 43 57 65 75 81 92 QuckSort(S 1 ) and QuckSort(S 2 ) Presto! S s sorted 4 6 3 8 1 9 2 5 Dvde 5 4 2 3 1 6 9 8 Dvde 2 1 3 4 6 8 9 Dvde 1 element 3 4 Conquer Conquer 1 2 3 4 6 8 9 Conquer 1 2 3 4 5 6 8 9 [Wess] 3 4 3 4
Pvot Pckng and Parttonng Pckng the Pvot The trcky peces are: Pckng the pvot Goal: pck a pvot value that wll cause S 1 and S 2 to be roughly equal n sze. Parttonng Preferably n-place Dealng wth duplcates. 5 6 Medan of Three Pvot 0 1 2 3 4 5 6 7 8 9 8 1 4 9 6 3 5 2 7 0 medanof3pvot( ) 0 1 4 9 7 3 5 2 6 8 Choose the pvot as the medan of three. Place the pvot and the largest at the rght and the smallest at the left. 7 Qucksort Parttonng Need to partton the array nto left and rght sub-arrays such that: elements n left sub-array are pvot elements n rght sub-array are pvot Can be done n-place wth another two ponter method Sounds lke mergesort, but here we are parttonng, not sortng and we can do t n-place. 8
Partonng In-place Setup: = start and = end of un-partoned elements: 0 1 4 9 7 3 5 2 6 8 Advance : Advance : Partonng In-place 0 1 4 2 7 3 5 9 6 8 0 1 4 2 7 3 5 9 6 8 Advance untl element pvot: 0 1 4 9 7 3 5 2 6 8 Swap : Advance : 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 7 9 6 8 Advance untl element pvot: 0 1 4 9 7 3 5 2 6 8 If >, then swap: 0 1 4 2 7 3 5 9 6 8 9 Advance : >, swap n pvot, partton done! 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 6 9 7 8 S 1 pvot pvot S 2 pvot 10 Partton Pseudocode Partton(A[], left, rght) { v = A[rght]; // Assumes pvot value currently at rght = left; // Intalze left sde, rght sde ponters = rght-1; Qucksort Pseudocode Puttng the peces together: // Do ++, -- untl they cross, swappng values as needed whle (1) { whle (A[] < v) ++; whle (A[] > v) --; f ( < ) { Swap(A[], A[]); ++; --; else break; Swap(A[], A[rght]); // Swap pvot value nto poston return ; // Return the fnal pvot poston Qucksort(A[], left, rght) { f (left < rght) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); 11 12 Complexty for nput sze n?
QuckSort: Best case complexty Qucksort(A[], left, rght) { f (left < rght) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); QuckSort: Worst case complexty Qucksort(A[], left, rght) { f (left < rght) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); 13 14 QuckSort: Average case complexty Turns out to be O(n log n). See Secton 7.7.5 for an dea of the proof. Don t need to know proof detals for ths course. Many Duplcates? An mportant case to consder s when an array has many duplcates. 0 1 2 3 4 5 6 7 8 9 8 6 6 6 6 6 6 6 6 0 medanof3pvot( ) 15 16
Parttonng wth Duplcates Setup: = start and = end of un-partoned elements: Advance untl element pvot: Parttonng wth Duplcates Advance,: Swap: Advance,: Advance untl element pvot: If >, then swap: 17 Swap: Advance,: Fnsh: 18 Parttonng wth Duplcates:Take Two Start = start and = end of un-partoned elements: Advance untl element > pvot (and n bounds): Advance untl element < pvot (and n bounds): Fnsh: Parttonng wth Duplcates: Upshot It s better to stop advancng ponters when elements are equal to pvot, and then ust do swaps. Complexty of qucksort on an array of dentcal values? Can we do better? Is ths better? 19 20
Important Tweak Inserton sort s actually better than qucksort on small arrays. Thus, a better verson of qucksort: Qucksort(A[], left, rght) { f (rght left CUTOFF) { medanof3pvot(a, left, rght); pvotindex = Partton(A, left+1, rght-1); Qucksort(A, left, pvotindex 1); Qucksort(A, pvotindex + 1, rght); else { InsertonSort(A, left, rght); CUTOFF = 10 s reasonable. 21 Propertes of Qucksort O(N 2 ) worst case performance, but O(N log N) average case performance. Pure qucksort not good for small arrays. No teratve verson (wthout usng a stack). In-place, but uses auxlary storage because of recursve calls. Stable? Used by Java for sortng arrays of prmtve types. 22 How fast can we sort? Heapsort, Mergesort, and Bnary Tree Sort all have O(N log N) worst case runnng tme. These algorthms, along wth Qucksort, also have O(N log N) average case runnng tme. Permutatons Suppose you are gven N elements Assume no duplcates How many possble orderngs can you get? Example: a, b, c (N = 3) Can we do any better? 23 24
Permutatons How many possble orderngs can you get? Example: a, b, c (N = 3) (a b c), (a c b), (b a c), (b c a), (c a b), (c b a) 6 orderngs = 3 2 1 = 3! (.e., 3 factoral ) All the possble permutatons of a set of 3 elements For N elements N choces for the frst poston, (N-1) choces for the second poston,, (2) choces, 1 choce N(N-1)(N-2)L(2)(1)= N! possble orderngs Sortng Model Recall our basc sortng assumpton: We can only compare two elements at a tme. These comparsons prune the space of possble orderngs. We can represent these concepts n a 25 26 Decson Tree Decson Trees b < c a < c c < a < b b > c a > c c < a < b a < b,, c < a < b,,, c < b < a a > b c < a b < c c < b < a c > a b > c c < b < a A Decson Tree s a Bnary Tree such that: Each node = a set of orderngs.e., the remanng soluton space Each edge = 1 comparson Each leaf = 1 unque orderng How many leaves for N dstnct elements? Only 1 leaf has the orderng that s the desred correctly sorted arrangement The leaves contan all the possble orderngs of a, b, c. 27 28
Decson Tree Example Decson Trees and Sortng b < c a < c c < a < b b > c a > c c < a < b a < b,, c < a < b,,, c < b < a actual order a > b c < a possble orders b < c c < b < a c > a b > c c < b < a Every sortng algorthm corresponds to a decson tree Fnds correct leaf by choosng edges to follow e, by makng comparsons We wll focus on worst case run tme. Observatons: Worst case run tme s maxmum number of comparsons. Maxmum number of comparsons s the length of the longest path n the decson tree,.e. the heght of the tree. 29 30 How many leaves on a tree? Suppose you have a bnary tree of heght h. How many leaves n a perfect tree? Lower bound on Heght A bnary tree of heght h has at most 2 h leaves Can prove formally by nducton A decson tree has N! leaves. What s ts mnmum heght of that tree? We can prune a perfect tree to make any bnary tree of same heght. Can # of leaves ncrease? 31 32
An Alternatve Explanaton Lower Bound on log(n!) At each decson pont, one branch has ½ of the optons remanng, the other has ½ remanng. Worst case: we always end up wth ½ remanng. Best algorthm, n the worst case: we always end up wth exactly ½ remanng. Thus, n the worst case, the best we can hope for s halvng the space d tmes (wth d comparsons), untl we have an answer,.e., untl the space s reduced to sze = 1. The space starts at N! n sze, and halvng d tmes means multplyng by 1/2 d, gvng us a lower bound on the worst case: N! d = 1 N! = 2 d= log 2( N!) d 2 33 34 Ω(N log N) Worst case run tme of any comparsonbased sortng algorthm s Ω(N log N). Can also show that average case run tme s also Ω(N log N). Can we do better f we don t use comparsons? (Huh?) 35