abstract in place merge abstract in place merge
play

Abstract in-place merge Abstract in-place merge Goal. Given two - PowerPoint PPT Presentation

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


  1. 
 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

  2. 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

  3. 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

  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] . 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

  5. 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

  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] . 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

  7. 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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend