1 / 10

Merge Sort

Merge Sort. Merge Sort Algorithm. The idea behind merge sort is divide and conquer Divide data into 2 equal parts Recursively sort both halves Merge the results. Merge Sort Example. Divide data into 2 equal parts Recursively sort both halves Merge the results. Merge Sort Example.

neola
Download Presentation

Merge Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Merge Sort

  2. Merge Sort Algorithm • The idea behind merge sort is divide and conquer • Divide data into 2 equal parts • Recursively sort both halves • Merge the results

  3. Merge Sort Example Divide data into 2 equal parts Recursively sort both halves Merge the results

  4. Merge Sort Example • 8 45 87 34 28 45 2 32 25 78 • Divide in half • 8 45 87 34 28 | 45 2 32 25 78 • Divide each half in half • 8 45 87 | 34 28 | 45 2 32 | 25 78 • Divide each half of half in half • 8 45 | 87 | 34 | 28 | 45 2 | 32 | 25 | 78 • Divide remaining elements that have more than one number in half • 8 | 45 | 87 | 34 | 28 | 45 | 2 | 32 | 25 | 78

  5. Merge Sort Example (continued) • 8 | 45 | 87 | 34 | 28 | 45 | 2 | 32 | 25 | 78 • Sortby comparing adjacent elements • 8 | 15  8 15 • 87  87 (not sorted) • 34 | 28  28 34 • 45 | 2  2 45 • 32  32 (not sorted) • 25 | 78  25 78 • 8 45 | 87 | 28 34 | 2 45 | 32 | 25 78

  6. Merge Sort Example (continued) • 8 45 | 87 | 28 34 | 2 45 | 32 | 25 78 • Sort by comparing adjacent elements • 8 45 | 87  8 45 87 • 28 34  28 34 (not sorted) • 2 45 | 32  2 32 45 • 25 78  25 78 (not sorted) • 8 45 87 | 28 34 | 2 32 45 | 25 78

  7. Merge Sort Example (continued) • 8 45 87 | 28 34 | 2 32 45 | 25 78 • Sort by comparing adjacent elements • 8 45 87 | 28 34  8 28 34 45 87 • 2 32 45 | 25 78  2 25 32 45 78 • 8 28 34 45 87 | 2 25 32 45 78 • Sort by comparing adjacent elements • 8 28 34 45 87 | 2 25 32 45 78 2 8 25 28 32 34 45 45 78 87 • 2 8 25 28 32 34 45 45 78 87

  8. Merge Sort Example (finished) • 8 45 87 34 28 45 2 32 25 78 • 8 45 87 34 28 | 45 2 32 25 78 • 8 45 87 | 34 28 | 45 2 32 | 25 78 • 8 45 | 87 | 34 | 28 | 45 2 | 32 | 25 | 78 • 8 | 45 | 87 | 34 | 28 | 45 | 2 | 32 | 25 | 78 • 8 45 | 87 | 28 34 | 2 45 | 32 | 25 78 • 8 45 87 | 28 34 | 2 32 45 | 25 78 • 8 28 34 45 87 | 2 25 32 45 78 • 2 8 25 28 32 34 45 45 78 87

  9. How to Sort? • After splitting the array, how do you sort? • Multiple options • Selection Sort • Insertion Sort • Other sorting algorithms

  10. Merge Sort Efficiency • O(n log n) time complexity • Better than selection sort and insertion sort for large arrays • Why use it? • Good option if the data to be sorted can only be efficiently accessed sequentially • Some data structures have elements that can only be accessed by going in order (i.e. can’t just skip to the middle or end easily)

More Related