Algorithms ROBERT SEDGEWICK | KEVIN WAYNE
Total Page:16
File Type:pdf, Size:1020Kb
Algorithms ROBERT SEDGEWICK | KEVIN WAYNE 2.2 MERGESORT ‣ mergesort ‣ bottom-up mergesort Algorithms ‣ sorting complexity FOURTH EDITION ‣ divide-and-conquer ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Last updated on 9/17/20 9:57 AM Two classic sorting algorithms: mergesort and quicksort Critical components in the world’s computational infrastructure. Full scientific understanding of their properties has enabled us to develop them into practical system sorts. Quicksort honored as one of top 10 algorithms of 20th century in science and engineering. Mergesort. [this lecture] ... Quicksort. [next lecture] ... 2 2.2 MERGESORT ‣ mergesort ‣ bottom-up mergesort Algorithms ‣ sorting complexity ‣ divide-and-conquer ROBERT SEDGEWICK | KEVIN WAYNE https://algs4.cs.princeton.edu Mergesort Basic plan. Divide array into two halves. Recursively sort each half. Merge two halves. input M E R G E S O R T E X A M P L E sort left half E E G M O R R S T E X A M P L E sort right half E E G M O R R S A E E L M P T X merge results A E E E E G L M M O P R R S T X Mergesort overview 4 Abstract in-place merge demo Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi], replace with sorted subarray a[lo] to a[hi]. lo mid mid+1 hi a[] E E G M R A C E R T sorted sorted 5 Mergesort: Transylvanian–Saxon folk dance Basic plan: Divide array into two halves. Recursively sort each half. Merge two halves. https://www.youtube.com/watch?v=XaqR3G_NVoo 6 Merging: Java implementation private static void merge(Comparable[] a, Comparable[] aux, int lo, int mid, int hi) { for (int k = lo; k <= hi; k++) copy aux[k] = a[k]; int i = lo, j = mid+1; merge for (int k = lo; k <= hi; k++) { if (i > mid) a[k] = aux[j++]; else if (j > hi) a[k] = aux[i++]; else if (less(aux[j], aux[i])) a[k] = aux[j++]; else a[k] = aux[i++]; } } lo i mid j hi aux[] A G L O R H I M S T k a[] A G H I L M 7 Mergesort quiz 1 How many calls does merge() make to less() in order to merge two sorted subarrays, each of length n / 2, into a sorted array of length n? A. ~ ¼ n to ~ ½ n merging two sorted arrays, each of length n/2 B. ~ ½ n a0 a1 a2 a3 b0 b1 b2 b3 C. ~ ½ n to ~ n D. ~ n best-case input (n/2 compares) A B C D E F G H worst-case input (n - 1 compares) A B C H D E F G 8 Mergesort: Java implementation public class Merge { private static void merge(...) { /* as before */ } private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi) { if (hi <= lo) return; int mid = lo + (hi - lo) / 2; sort(a, aux, lo, mid); sort(a, aux, mid+1, hi); merge(a, aux, lo, mid, hi); } public static void sort(Comparable[] a) { avoid array allocation Comparable[] aux = new Comparable[a.length]; in inner loop sort(a, aux, 0, a.length - 1); } } lo mid hi 10 11 12 13 14 15 16 17 18 19 9 Mergesort: trace a[] a[] lo hi 0 1 2 3 4 5 a[] 6 7 8 9 10 11 12 13 14 15 lo hi 0 1 2 3 4 5 a[] 6 7 8 9 10 11 12 13 14 15 lo hi 0M 1E 2R 3G E 4 S 5 a[] O6 R7 T8 E9 10 X 11 A 12 M 13P 14L 15E lo hi 0M 1E 2R 3G E4 S5 a[] O6 R7 T8 E9 10 X 11 A 12 M 13P 14L 15E merge(a, aux, lo 0 , 0, 1 hi) E 0M M1E R2 G3 E 4 S 5 a[] O6 R7 T8 E9 10X 11A 12M 13P 14L 15E result after recursive call merge(a, aux, lo 0 , 0, 1hi ) 0ME 1EM R2 G3 E4 S5 a[] O6 R7 T8 E9 10X 11A 12M 13P 14L 15E merge(a, aux, lo 0 , 0, 1 hi) E 0M M1E R2 G3 E 4 S 5 a[] O6 R7 T8 E9 10X 11A 12M 13P 14L 15E merge(a, aux, lo 20 , 2,0, 3 1hi ) 0EM 1ME R2G G3R E4 S5 O6 R7 T8 E9 10X 11A 12M 13P 14L 15E merge(a, aux, lo 0 2, 0, 2, 31 hi) E0M M1E R2G G3R E4 S5 O6 R7 T8 E9 10X 11A 12M 13P 14L 15E merge(a, merge(a, aux, aux, 0 ,20 , 1, 2,0, 3 )3 1) EM0 MEG1 RGM2 GR3 E4 S5 O6 R7 T8 E9 10X 11A 12M 13P 14L 15E merge(a, merge(a, aux, aux, 0 ,0 2, 1, 0, 2, 3 ) 31) EM MGE RMG GR E S O R T E X A M P L E merge(a, merge(a, aux, aux, 0 ,240 , 1, 2,4,0, 3 )35 1) EM MGE RGM GR E S O R T E X A M P L E merge(a, merge(a, aux, aux, 0 ,0 24, 1, 0, 2,4, 3 ) 351) E MG RMG GR E S O R T E X A M P L E merge(a, merge(a, aux, aux, 0 ,240 , 1, 2,4,0, 3 )35 1) E MG RGM GR E S O R T E X A M P L E merge(a, merge(a, aux, aux, 0 , 6240 ,1, 6,2,4,0, 3 ) 7351 ) E GM RMG GR E S O R T E X A M P L E merge(a, merge(a, aux, aux, 0 ,24 6, 1, 2,4, 6, 3 )35 7) E MG GM R E S O R T E X A M P L E merge(a, merge(a, aux, aux, 04 , 624 ,1, 5, 6,2,4, 3 7) 735 ) E GM MG R E SO OR RS T E X A M P L E merge(a, merge(a, aux, aux, 40, 4 6, 5,1, 4, 6, 73 )5 7) E G M R E SO OR RS T E X A M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 0 4, 63,4 ,1, 5, 6,74, )3 7) 75 ) E GE MG RM EO SRO OR RS T E X A M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4, 4 63,, 5, 4, 6,7 ) 7 )5 7) E GE MG RM EO SOR OR RS T E X A M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4 ,63,4 , 5, 6,74, ) 7 )7 5) E GE MG RM EO SRO OR RS T E X A M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4 , 863,4 ,5, 8,6,74, )7 ) 975 ) E GE MG RM EO ORS RO SR TE ET X A M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4 ,63, 8, 5, 6,7 8,) 7 )7 9) E GE MG RM EO SRO OR RS TE ET X A M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4 , 10 863,, 5, 10, 8,6,7 )7 ) 11 97 ) E GE MG RM EO ORS RO SR TE ET XA AX M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4 10,3, 8, 5, 10,7 8,) 7 11) 9 ) E EG GM MR OE RO R S TE TE XA AX M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 4 8, 10 83,, 5, 9, 10, 8,7 11)7 ) 11 9 ) E GE MG RM EO OR R S TEA ET XAT AX M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 8 10,3,8 , 9, 10,78, )11 11) 9 ) E E G M O R R S ETA TE XAT AX M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 8, 108 3,, 9, 10,8, 711) )119 ) E E G M O R R S EAT TE XAT AX M P L E merge(a, merge(a, merge(a, aux, aux, aux, 0 , 81012, 8 3,, 9,10,12, 8, 711) 1113 )9 ) E E G M O R R S EAT TE XAT AX M P L E merge(a, merge(a, aux, aux, 8,1012 8 , 9, 10,12,8, 11 )11139 ) E E G M O R R S EA TE XAT AX M P L E merge(a, merge(a, aux, aux, 81012,14 8 , 9,10,12, 14,8, 11 1113 )159 ) E E G M O R R S EA TE XAT AX M P LE EL merge(a, merge(a, aux, aux, 8 ,141012 , 9, 14,10,12, 11 )151113 ) E E G M O R R S AE ET TA X M P EL LE merge(a, merge(a, aux, aux, 12 81012,14 ,13, 9,10,12,14, 1115 1113)15 ) E E G M O R R S EA TE AT X ME PL LEM ELP merge(a, merge(a, aux, aux, 12 8 ,1412 , 13,9, 14,12, 1115 )1513 ) E E G M O R R S A E T X EM LP ELM LEP merge(a, merge(a, aux, aux, 12 812,14 ,13, 9,12,14, 1115 13)15 ) E E G M O R R S A E T X EM LP ELM LEP merge(a, merge(a, merge(a, aux, aux, aux, 8 ,12 81411,12, ,13, 9,14,1512, )1115 15 13) ) E E G M O R R S A E TE XL EM LP ELMT LEPX merge(a, merge(a, merge(a, aux, aux, aux, 8 12, 12,1411, ,13, 12,14,15 )15 13)15 ) E E G M O R R S A E TE XL EM LP ELMT LEPX merge(a, merge(a, merge(a, merge(a, aux, aux, aux, 0aux, , 8 ,12 7,1411,12, ,13,15 14,1512,) ) 15 15 13) ) EA E GE ME OE RG RL SM AM EO TPE RXL EMR LPS ELMT LEPX merge(a, merge(a, merge(a, merge(a, aux, aux, aux, 0aux, , 812 ,7, ,1411, ,13,15 )14,15 )15 ) 15 ) EA E GE ME OE RG LR MS MA OE PTE RXL ERM LSP EMT LPX merge(a, merge(a, merge(a, merge(a, aux, aux, aux, 0aux, , 8 ,12 7,1411,, ,13,15 14,15) ) 15 15 ) ) EA E GE ME OE RG RL SM AM EO TPE RXL EMR LPS EMT LPX merge(a, merge(a, merge(a, aux, aux, aux, 0 , 812 ,7, ,11, 13,15 )15 )15Trace ) of merge EA Eresults GE forME top-down OE RG LRmergesort MS MA OE PTE RXL ERM LSP MT PX merge(a, merge(a, merge(a, aux, aux, aux, 0 , 8 ,12 7,11,, 13,15 15) ) 15Trace ) of merge EA Eresults GE forME top-down OE RG RLmergesort SM MA OE PET RLX EMR LPS MT PX merge(a, merge(a, merge(a, aux, aux, aux, 0 , 8 ,127, 11,, 1513, )15 )15Trace ) of merge AE Eresults EG forEM top-down EO GR LRmergesort MS MA OE PET RLX ERM LSP MT PX merge(a, merge(a, aux, aux, 0 , 8 , 7,11, 15 15) ) Trace of merge EA Eresults GE forME top-down OE RG RLmergesort SM MA OE PE RL MR PS T X merge(a, merge(a, aux, aux, 0 , 8 ,7, 11, 15 )15 )Trace of merge AE Eresults EG forEM top-down EO GR LRmergesort MS MA OE PE RL RM SP T X merge(a, aux, 0, 7, 15) Trace of merge A Eresults E forE top-down E G Lmergesort M M O P R R S T X merge(a, aux, 0, 7, 15) Trace of merge A Eresults E forE top-down E G Lmergesort M M O P R R S T X Trace of merge results for top-down mergesort Trace of merge results for top-down mergesort 10 Mergesort quiz 2 Which subarray lengths will arise when mergesorting an array of length 12? A.