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

Programming and Data Structures1Merge 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 trivialProgramming and Data Structures2Merge 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,

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

Programming and Data Structures3Merge 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 1so we must take this into account in our algorithm design.Programming and Data Structures4Merge Sort – Visual Example75 55 15 20 85 30 35 10 60 40 50 25 45 80 70 65 75 55 15 20 85 30 35 1060 40 50 25 45 80 70 65 • Divide list into 2 sublist, so at next level of recursion we have :Sub-list 1Sub-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 bemerged together. 75551520853035106040502545807065