BBM 202 - ALGORITHMS D EPT . OF C OMPUTER E NGINEERING Q UICKSORT Feb. 27, 2017 Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick and K. Wayne of Princeton University.
Quicksort Basic plan. • Shuffle the array. • Partition so that, for some j - entry a[j] is in place - no larger entry to the left of j - no smaller entry to the right of j • Sort each piece recursively. Sir Charles Antony Richard Hoare 1980 Turing Award Q U I C K S O R T E X A M P L E input K R A T E L E P U I M Q C X O S shu ffm e partitioning item E C A I E K L P U T M Q R X O S partition not greater not less A C E E I K L P U T M Q R X O S sort left A C E E I K L M O P Q R S T U X sort right A C E E I K L M O P Q R S T U X result 2
Shuffling Shuffling • Shuffling is the process of rearranging an array of elements randomly. • A good shuffling algorithm is unbiased, where every ordering is equally likely. • e.g. the Fisher–Yates shuffle (aka. the Knuth shuffle) http://bl.ocks.org/mbostock/39566aca95eb03ddd526 3
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K R A T E L E P U I M Q C X O S lo i j stop i scan because a[i] >= a[lo] 4
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K R A T E L E P U I M Q C X O S lo i j 5
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K R A T E L E P U I M Q C X O S lo i j 6
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K R A T E L E P U I M Q C X O S lo i j stop j scan and exchange a[i] with a[j] 7
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A T E L E P U I M Q R X O S lo i j 8
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A T E L E P U I M Q R X O S lo i j 9
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A T E L E P U I M Q R X O S lo i j stop i scan because a[i] >= a[lo] 10
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A T E L E P U I M Q R X O S lo i j 11
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A T E L E P U I M Q R X O S lo i j 12
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A T E L E P U I M Q R X O S lo i j stop j scan and exchange a[i] with a[j] 13
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E L E P U T M Q R X O S lo i j 14
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E L E P U T M Q R X O S lo i j 15
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E L E P U T M Q R X O S lo i j stop i scan because a[i] >= a[lo] 16
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E L E P U T M Q R X O S lo i j 17
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E L E P U T M Q R X O S lo i j 18
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E L E P U T M Q R X O S lo i j stop j scan and exchange a[i] with a[j] 19
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E E L P U T M Q R X O S lo i j 20
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E E L P U T M Q R X O S lo i j stop i scan because a[i] >= a[lo] 21
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . K C A I E E L P U T M Q R X O S lo j i stop j scan because a[j] <= a[lo] 22
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . When pointers cross. • Exchange a[lo] with a[j] . K C A I E E L P U T M Q R X O S lo j i pointers cross: exchange a[lo] with a[j] 23
Quicksort partitioning Repeat until i and j pointers cross. • Scan i from left to right so long as a[i] < a[lo] . • Scan j from right to left so long as a[j] > a[lo] . • Exchange a[i] with a[j] . When pointers cross. • Exchange a[lo] with a[j] . E C A I E K L P U T M Q R X O S lo j hi partitioned! 24
Quicksort partitioning Basic plan. • Scan i from left for an item that belongs on the right. • Scan j from right for an item that belongs on the left. • Exchange a[i] and a[j] . • Repeat until pointers cross. v a[i] i j 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 16 K R A T E L E P U I M Q C X O S initial values 1 12 K R A T E L E P U I M Q C X O S scan left, scan right 1 12 K C A T E L E P U I M Q R X O S exchange 3 9 K C A T E L E P U I M Q R X O S scan left, scan right 3 9 K C A I E L E P U T M Q R X O S exchange 5 6 K C A I E L E P U T M Q R X O S scan left, scan right 5 6 K C A I E E L P U T M Q R X O S exchange 6 5 K C A I E E L P U T M Q R X O S scan left, scan right 6 5 E C A I E K L P U T M Q R X O S fj nal exchange 6 5 E C A I E K L P U T M Q R X O S result Partitioning trace (array contents before and after each exchange) 25
Quicksort: Java code for partitioning private static int partition(Comparable[] a, int lo, int hi) { int i = lo, j = hi+1; while (true) { while (less(a[++i], a[lo])) find item on left to swap if (i == hi) break; while (less(a[lo], a[--j])) find item on right to swap if (j == lo) break; check if pointers cross if (i >= j) break; exch(a, i, j); swap } exch(a, lo, j); swap with partitioning item return j; return index of item now known to be in place } i j after � v � v v during � v � v v v before lo j hi lo hi i j 26
Quicksort: Java implementation public class Quick { private static int partition(Comparable[] a, int lo, int hi) { /* see previous slide */ } public static void sort(Comparable[] a) { shuffle needed for StdRandom.shuffle(a); performance guarantee sort(a, 0, a.length - 1); (stay tuned) } private static void sort(Comparable[] a, int lo, int hi) { if (hi <= lo) return; int j = partition(a, lo, hi); sort(a, lo, j-1); sort(a, j+1, hi); } } 27
Recommend
More recommend