CSE 326: Data Structures Qucksort Comparson Sortng Bound Bran Curless Sprng 2008 Announcements (5/14/08) Homework due at begnnng of class on Frday. Secton tomorrow: Graded homeworks returned More dscusson of proect, Java and hash tables Readng for ths lecture: Chapter 7. 2 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 The steps of Qucksort length 0 or 1. 3 [Wess] 4 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
Qucksort Example Pvot Pckng and Parttonng 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 3 4 Conquer 1 2 3 4 6 8 9 Conquer 1 2 3 4 5 6 8 9 5 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. 6 Pckng the Pvot 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. 7 Place the pvot and the largest at the rght and the smallest at the left. 8
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. 9 Partonng In-place Setup: = start and = end of un-partoned elements: 0 1 4 9 7 3 5 2 6 8 Advance untl element pvot: 0 1 4 9 7 3 5 2 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 10 Advance : Advance : Swap : Advance : Advance : >, swap n pvot, partton done! Partonng In-place 0 1 4 2 7 3 5 9 6 8 0 1 4 2 7 3 5 9 6 8 0 1 4 2 5 3 7 9 6 8 0 1 4 2 5 3 7 9 6 8 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 11 Partton Pseudocode Partton(A[], left, rght) { v = A[rght]; // Assumes pvot value currently at rght = left; // Intalze left sde, rght sde ponters = rght-1; // 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 Complexty for nput sze n? 12
Qucksort Pseudocode Puttng the peces together: 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: Best 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); 13 14 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, pvotindex 1); Qucksort(A, pvotindex + 1, rght); 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. 15 16
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( ) Parttonng wth Duplcates Setup: = start and = end of un-partoned elements: Advance untl element pvot: Advance untl element pvot: 17 If >, then swap: 18 Parttonng wth Duplcates Advance,: Swap: Advance,: Parttonng wth Duplcates:Take Two Start = start and = end of un-partoned elements: Advance untl element > pvot (and n bounds): Swap: Advance,: Fnsh: 19 Advance untl element < pvot (and n bounds): Fnsh: Is ths better? 20
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? 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); Can we do better? 21 else { InsertonSort(A, left, rght); CUTOFF = 10 s reasonable. 22 Propertes of Qucksort How fast can we sort? 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. 23 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. Can we do any better? 24
Sortng Model Recall our basc assumpton: we can only compare two elements at a tme we can only reduce the possble soluton space by half each tme we make a comparson Suppose you are gven N elements Assume no duplcates How many possble orderngs can you get? Example: a, b, c (N = 3) 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) (2)(1)= N! possble orderngs 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 29 Every sortng algorthm corresponds to a decson tree Fnds correct leaf by choosng edges to follow e, by makng comparsons Each decson reduces the possble soluton space by one half 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. 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
Lower Bound on log(n!) Ω(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?) 33 34