SlideShare une entreprise Scribd logo
1  sur  16
Mergesort
Merging two sorted arrays
• To merge two sorted arrays into a third (sorted)
  array, repeatedly compare the two least elements
  and copy the smaller of the two


          7    9   14 18      5   10 12 20




           5   7    9   10 12 14 18 20
Merging parts of a single array
• The procedure really is no different if the two
  “arrays” are really portions of a single array

               first part          second part

           7    9    14 18     5     10 12 20




           5    7    9      10 12 14 18 20
Sorting an array
• If we start with an array in which the left half is
  sorted and the right half is sorted, we can merge
  them into a single sorted array
• We need to copy our temporary array back up

            7    9   14 18    5   10 12 20




            7    9   14 18    5   10 12 20

• This is a sorting technique called mergesort
Recursion
• How did the left and right halves of our array get
  sorted?
• Obviously, by mergesorting them
• To mergesort an array:
   – Mergesort the left half
   – Mergesort the right half
   – Merge the two sorted halves
• Each recursive call is with a smaller portion of the
  array
• The base case: Mergesort an array of size 1
The actual code
private void recMergeSort(long[] workspace,
                         int lowerBound,
                         int upperBound) {
    if (lowerBound == upperBound) return;
    int mid = (lowerBound + upperBound) / 2;
    recMergeSort(workspace, lowerBound, mid);
    recMergeSort(workspace, mid + 1, upperBound);
    merge(workspace, lowerBound, mid + 1, upperBound);
}
                                  from Lafore, p. 287
The merge method
• The merge method is too ugly to show
• It requires:
   – The index of the first element in the left subarray
   – The index of the last element in the right subarray
   – The index of the first element in the right subarray
      • That is, the dividing line between the two subarrays
   – The index of the next value in the left subarray
   – The index of the next value in the right subarray
   – The index of the destination in the workspace
• But conceptually, it isn’t difficult!
Analysis of the merge operation
• The basic operation is: compare two elements,
  move one of them to the workspace array
   – This takes constant time
• We do this comparison n times
   – Hence, the whole thing takes O(n) time
• Now we move the n elements back to the original
  array
   – Each move takes constant time
   – Hence moving all n elements takes O(n) time
• Total time: O(n) + O(n) = O(n)
But wait...there’s more
• So far, we’ve found that it takes O(n) time to merge two
  sorted halves of an array
• How did these subarrays get sorted?
• At the next level down, we had four subarrays, and we
  merged them pairwise




• Since merging arrays is linear time, when we cut the array
  size in half, we cut the time in half
• But there are two arrays of size n/2
• Hence, all merges combined at the next lower level take the
  same amount of time as a single merge at the upper level
Analysis II
• So far we have seen that it takes
   – O(n) time to merge two subarrays of size n/2
   – O(n) time to merge four subarrays of size n/4 into two
     subarrays of size n/2
   – O(n) time to merge eight subarrays of size n/8 into four
     subarrays of size n/4
   – Etc.
• How many levels deep do we have to proceed?
• How many times can we divide an array of size n
  into two halves?
   – log2n, of course
Analysis III
• So if our recursion goes log n levels deep...
• ...and we do O(n) work at each level...
• ...our total time is: log n * O(n)...
• ...or in other words,
          O(n log n)
• For large arrays, this is much better than
  Bubblesort, Selection sort, or Insertion sort, all of
  which are O(n2)
• Mergesort does, however, require a “workspace”
  array as large as our original array
Stable sort algorithms
• A stable sort keeps
  equal elements in     Ann 98     Ann 98
  the same order        Bob 90     Joe 98
• This may matter       Dan 75     Bob 90
  when you are
  sorting data          Joe 98     Sam 90
  according to some     Pat 86     Pat 86
  characteristic        Sam 90     Zöe 86
• Example: sorting
                        Zöe 86     Dan 75
  students by test
  scores                original   stably
                        array      sorted
Unstable sort algorithms
• An unstable sort
                         Ann 98     Joe 98
  may or may not
  keep equal             Bob 90     Ann 98
  elements in the        Dan 75     Bob 90
  same order             Joe 98     Sam 90
• Stability is usually   Pat 86     Zöe 86
  not important, but
                         Sam 90     Pat 86
  sometimes it
  matters                Zöe 86     Dan 75
                         original   unstably
                         array      sorted
Is mergesort stable?
private void recMergeSort(long[] workspace,
                            int lowerBound,
                            int upperBound) {
    if (lowerBound == upperBound) return;
    int mid = (lowerBound + upperBound) / 2;
    recMergeSort(workspace, lowerBound, mid);
    recMergeSort(workspace, mid + 1, upperBound);
    merge(workspace, lowerBound, mid + 1, upperBound);
}
• Is this sort stable?
• recMergeSort does none of the actual work of
  moving elements around—that’s done in merge
Stability of merging
• Stability of mergesort depends on the merge method
• After all, this is the only place where elements get moved

          7     9     14 18            5    10 12 20




            5     7     9    10 12 14 18 20

• What if we encounter equal elements when merging?
   – If we always choose the element from the left subarray,
     mergesort is stable
The End

Contenu connexe

Tendances

358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14sumitbardhan
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sortingKrish_ver2
 
10 merge sort
10 merge sort10 merge sort
10 merge sortirdginfo
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureTushar Gonawala
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sortingryokollll
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderAshin Guha Majumder
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting AlgorithmAl Amin
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap SortMohammed Hussein
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting TechniquesRafay Farooq
 
Selection sort
Selection sortSelection sort
Selection sortasra khan
 

Tendances (20)

358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14358 33 powerpoint-slides_14-sorting_chapter-14
358 33 powerpoint-slides_14-sorting_chapter-14
 
3.9 external sorting
3.9 external sorting3.9 external sorting
3.9 external sorting
 
Sorting
SortingSorting
Sorting
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Sorting
SortingSorting
Sorting
 
10 merge sort
10 merge sort10 merge sort
10 merge sort
 
Different Sorting tecniques in Data Structure
Different Sorting tecniques in Data StructureDifferent Sorting tecniques in Data Structure
Different Sorting tecniques in Data Structure
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Lect11 Sorting
Lect11 SortingLect11 Sorting
Lect11 Sorting
 
Sorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha MajumderSorting Seminar Presentation by Ashin Guha Majumder
Sorting Seminar Presentation by Ashin Guha Majumder
 
Sorting Algorithm
Sorting AlgorithmSorting Algorithm
Sorting Algorithm
 
Quick Sort , Merge Sort , Heap Sort
Quick Sort , Merge Sort ,  Heap SortQuick Sort , Merge Sort ,  Heap Sort
Quick Sort , Merge Sort , Heap Sort
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Quicksort
QuicksortQuicksort
Quicksort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Sorting
SortingSorting
Sorting
 
Shell sort
Shell sortShell sort
Shell sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Mergesort
MergesortMergesort
Mergesort
 
Quick sort
Quick sortQuick sort
Quick sort
 

En vedette

En vedette (12)

Quicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk throughQuicksort: illustrated step-by-step walk through
Quicksort: illustrated step-by-step walk through
 
Heap sort
Heap sortHeap sort
Heap sort
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
Hashing and Hash Tables
Hashing and Hash TablesHashing and Hash Tables
Hashing and Hash Tables
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Heap sort
Heap sort Heap sort
Heap sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Hashing
HashingHashing
Hashing
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Heap sort
Heap sortHeap sort
Heap sort
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 

Similaire à 16 mergesort

sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptxAnSHiKa187943
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfArjunSingh81957
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxMohammed472103
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisRadhika Talaviya
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sortingVivek Bhargav
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Abdul Khan
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sortingFadhil Ismail
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfAkashSingh625550
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.pptLegesseSamuel
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 

Similaire à 16 mergesort (20)

sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
sorting-160810203705.pptx
sorting-160810203705.pptxsorting-160810203705.pptx
sorting-160810203705.pptx
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdfSorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
Sorting-algorithmbhddcbjkmbgjkuygbjkkius.pdf
 
Ch2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptxCh2 Part III-Advanced Sorting algorithms.pptx
Ch2 Part III-Advanced Sorting algorithms.pptx
 
Analysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysisAnalysis and Design of Algorithms -Sorting Algorithms and analysis
Analysis and Design of Algorithms -Sorting Algorithms and analysis
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Unit vii sorting
Unit   vii sorting Unit   vii sorting
Unit vii sorting
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
Algorithm & data structures lec4&5
Algorithm & data structures lec4&5Algorithm & data structures lec4&5
Algorithm & data structures lec4&5
 
Sorting
SortingSorting
Sorting
 
Quick sort
Quick sortQuick sort
Quick sort
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting(Data Structure) Chapter11 searching & sorting
(Data Structure) Chapter11 searching & sorting
 
Lec 03 - Sorting.pptx
Lec 03 - Sorting.pptxLec 03 - Sorting.pptx
Lec 03 - Sorting.pptx
 
Class13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdfClass13_Quicksort_Algorithm.pdf
Class13_Quicksort_Algorithm.pdf
 
Lec35
Lec35Lec35
Lec35
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Complexity in array
Complexity in arrayComplexity in array
Complexity in array
 

Plus de Xavient Information Systems (10)

Mobility in network
Mobility in networkMobility in network
Mobility in network
 
Software engineering
Software engineeringSoftware engineering
Software engineering
 
Binary searchtrees
Binary searchtreesBinary searchtrees
Binary searchtrees
 
Red black-trees-4
Red black-trees-4Red black-trees-4
Red black-trees-4
 
Train name index
Train name indexTrain name index
Train name index
 
Osi model
Osi modelOsi model
Osi model
 
Discrete math mathematics for computer science 2
Discrete math   mathematics for computer science 2Discrete math   mathematics for computer science 2
Discrete math mathematics for computer science 2
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
Harsh embedded systems
Harsh embedded systemsHarsh embedded systems
Harsh embedded systems
 
Harsh
HarshHarsh
Harsh
 

16 mergesort

  • 2. Merging two sorted arrays • To merge two sorted arrays into a third (sorted) array, repeatedly compare the two least elements and copy the smaller of the two 7 9 14 18 5 10 12 20 5 7 9 10 12 14 18 20
  • 3. Merging parts of a single array • The procedure really is no different if the two “arrays” are really portions of a single array first part second part 7 9 14 18 5 10 12 20 5 7 9 10 12 14 18 20
  • 4. Sorting an array • If we start with an array in which the left half is sorted and the right half is sorted, we can merge them into a single sorted array • We need to copy our temporary array back up 7 9 14 18 5 10 12 20 7 9 14 18 5 10 12 20 • This is a sorting technique called mergesort
  • 5. Recursion • How did the left and right halves of our array get sorted? • Obviously, by mergesorting them • To mergesort an array: – Mergesort the left half – Mergesort the right half – Merge the two sorted halves • Each recursive call is with a smaller portion of the array • The base case: Mergesort an array of size 1
  • 6. The actual code private void recMergeSort(long[] workspace, int lowerBound, int upperBound) { if (lowerBound == upperBound) return; int mid = (lowerBound + upperBound) / 2; recMergeSort(workspace, lowerBound, mid); recMergeSort(workspace, mid + 1, upperBound); merge(workspace, lowerBound, mid + 1, upperBound); } from Lafore, p. 287
  • 7. The merge method • The merge method is too ugly to show • It requires: – The index of the first element in the left subarray – The index of the last element in the right subarray – The index of the first element in the right subarray • That is, the dividing line between the two subarrays – The index of the next value in the left subarray – The index of the next value in the right subarray – The index of the destination in the workspace • But conceptually, it isn’t difficult!
  • 8. Analysis of the merge operation • The basic operation is: compare two elements, move one of them to the workspace array – This takes constant time • We do this comparison n times – Hence, the whole thing takes O(n) time • Now we move the n elements back to the original array – Each move takes constant time – Hence moving all n elements takes O(n) time • Total time: O(n) + O(n) = O(n)
  • 9. But wait...there’s more • So far, we’ve found that it takes O(n) time to merge two sorted halves of an array • How did these subarrays get sorted? • At the next level down, we had four subarrays, and we merged them pairwise • Since merging arrays is linear time, when we cut the array size in half, we cut the time in half • But there are two arrays of size n/2 • Hence, all merges combined at the next lower level take the same amount of time as a single merge at the upper level
  • 10. Analysis II • So far we have seen that it takes – O(n) time to merge two subarrays of size n/2 – O(n) time to merge four subarrays of size n/4 into two subarrays of size n/2 – O(n) time to merge eight subarrays of size n/8 into four subarrays of size n/4 – Etc. • How many levels deep do we have to proceed? • How many times can we divide an array of size n into two halves? – log2n, of course
  • 11. Analysis III • So if our recursion goes log n levels deep... • ...and we do O(n) work at each level... • ...our total time is: log n * O(n)... • ...or in other words, O(n log n) • For large arrays, this is much better than Bubblesort, Selection sort, or Insertion sort, all of which are O(n2) • Mergesort does, however, require a “workspace” array as large as our original array
  • 12. Stable sort algorithms • A stable sort keeps equal elements in Ann 98 Ann 98 the same order Bob 90 Joe 98 • This may matter Dan 75 Bob 90 when you are sorting data Joe 98 Sam 90 according to some Pat 86 Pat 86 characteristic Sam 90 Zöe 86 • Example: sorting Zöe 86 Dan 75 students by test scores original stably array sorted
  • 13. Unstable sort algorithms • An unstable sort Ann 98 Joe 98 may or may not keep equal Bob 90 Ann 98 elements in the Dan 75 Bob 90 same order Joe 98 Sam 90 • Stability is usually Pat 86 Zöe 86 not important, but Sam 90 Pat 86 sometimes it matters Zöe 86 Dan 75 original unstably array sorted
  • 14. Is mergesort stable? private void recMergeSort(long[] workspace, int lowerBound, int upperBound) { if (lowerBound == upperBound) return; int mid = (lowerBound + upperBound) / 2; recMergeSort(workspace, lowerBound, mid); recMergeSort(workspace, mid + 1, upperBound); merge(workspace, lowerBound, mid + 1, upperBound); } • Is this sort stable? • recMergeSort does none of the actual work of moving elements around—that’s done in merge
  • 15. Stability of merging • Stability of mergesort depends on the merge method • After all, this is the only place where elements get moved 7 9 14 18 5 10 12 20 5 7 9 10 12 14 18 20 • What if we encounter equal elements when merging? – If we always choose the element from the left subarray, mergesort is stable