8 - MergeSort

8 - MergeSort - Merge Sort • Sorting Schemes can be...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming and Data Structures 1 Merge Sort Sorting Schemes can be classified as internal (sorting data already in memory) and external (sorting collection of data from secondary storage). So far we have been looking at internal schemes. What if we want to sort, say for example records from files? Sorts covered so far are not the best way to do this. Inefficient, especially for large data sets like those we expect to see in files. Merge Sort is a sorting algorithm that is both useful for internal and external sorting. We will cover Merge Sort for internal sorting of array data for now but the extension to external sorting is trivial Programming and Data Structures 2 Merge Sort (2) • Many useful algorithms are recursive • These algorithms employ a divide and conquer approach. • Think of merging two lists of sorted numbers. • All that is needed is to look at the top items and add the appropriate one each time • The merge sort algorithm closely follows the Divide and Conquer paradigm. It operates as follows,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Programming and Data Structures 3 Merge Sort – Divide & Conquer Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each. Conquer: Sort the two subsequences recursively (I.e. by using merge sort again on the subsequences). Combine: Merge the two sorted subsequences to produce the sorted answer. This procedure will bottom out when the list to be sorted is of length 1 so we must take this into account in our algorithm design. Programming and Data Structures 4 Merge Sort – Visual Example 75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65 75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65 • Divide list into 2 sublist, so at next level of recursion we have : Sub-list 1 Sub-list 2 • We keep splitting our sublists into smaller sublists until we can’t split any further (This is a result of sorting our sublist before mergine by recursively calling ourselves). • Eventually we will end up with distinct pairs of already sorted sublists which need to be merged together. 75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65
Background image of page 2
Programming and Data Structures 5 Merge Sort – Visual Example (2) • After the first merge we end up with half as many sublists as before the merge : 55 75 15 20 30 85 10 35 40 60 25 50 45 80 65 70 • We again merge the susequent sorted sublists together again, thus reducing the number of sublists again to half : 15 20 55 75 10 30 35 85 25 40 50 60 45 65 70 80 • Repeating the same procedure again we end up with 2 sorted sublists to be merged : 10 15 20 30 35 55 75 85 25 40 45 50 60 65 70 80 • Merging these two sorted sublist together we end up with our original listed sorted! : 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 Programming and Data Structures 6 Merge Sort – Pseudo Code MergeSort (List[], leftIndex, rightIndex) Begin: if leftIndex < rightIndex then mid = (leftIndex + rightIndex) / 2 MergeSort(List[], leftIndex, mid) // split left sublist MergeSort(List[], mid + 1, rightIndex) // split right sublist Merge(List[], leftIndex, mid, rightIndex) // merge sorted sublists endif End • If we leave the merge procedure as a subroutine in the algorithm the MergeSort algorithm can be written very simply as :
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 15

8 - MergeSort - Merge Sort • Sorting Schemes can be...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online