Mergesort BBM 202 - ALGORITHMS Basic plan. • Divide array into two halves. • Recursively sort each half. D EPT . OF C OMPUTER E NGINEERING • 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 M ERGESORT merge results A E E E E G L M M O P R R S T X Mergesort overview Acknowledgement: The course slides are adapted from the slides prepared by R. Sedgewick 2 and K. Wayne of Princeton University. Abstract in-place merge 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 E E G M R A C E R T a[] sorted sorted 3 4
Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . lo mid mid+1 hi E E E E G G M M R R A A C C E E R R T T E E G M R A C E R T a[] a[] copy to auxiliary array E E G M R A C E R T aux[] aux[] 5 6 Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . E E G M R A C E R T E A E G M R A C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E G M R A A C E R T E E G M R A C E R T aux[] aux[] i j i j 7 8
Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A E G M R A C E R T A E C G M R A C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E G M R A C C E R T E E G M R A C E R T aux[] aux[] i j i j 9 10 Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C G M R A C E R T A C G E M R A C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E E G M R A C E R T E E G M R A C E R T aux[] aux[] i j i j 11 12
Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E M R A C E R T A C E M E R A C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E E G M R A C E R T E E G M R A C E R T aux[] aux[] i j i j 13 14 Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E E R A C E R T A C E E R E A C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E G M R A C E E R T E E G M R A C E R T aux[] aux[] i j i j 15 16
Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E E E A C E R T A C E E E G A C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E G G M R A C E R T E E G M R A C E R T aux[] aux[] i j i j 17 18 Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E E E G C E R T A C E E E G M C E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E G M M R A C E R T E E G M R A C E R T aux[] aux[] i j i j 19 20
Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E E E G M E R T A C E E E G M R E R T a[] a[] k k compare minimum in each subarray compare minimum in each subarray E E G M R R A C E R T E E G M R A C E R T aux[] aux[] i j i j 21 22 Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E E E G M R R T A C E E E G M R R R T a[] a[] k k one subarray exhausted, take from other one subarray exhausted, take from other E E G M R A C E R R T E E G M R A C E R T aux[] aux[] i j i j 23 24
Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . A C E E E G M R R T A C E E E G M R R T T a[] a[] k k one subarray exhausted, take from other one subarray exhausted, take from other E E G M R A C E R T T E E G M R A C E R T aux[] aux[] i j i j 25 26 Abstract in-place merge Abstract in-place merge Goal. Given two sorted subarrays a[lo] to a[mid] and a[mid+1] to a[hi] , replace with 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] . sorted subarray a[lo] to a[hi] . lo hi A C E E E G M R R T A C E E E G M R R T a[] a[] k sorted both subarrays exhausted, done E E G M R A C E R T aux[] i j 27 28
Recommend
More recommend