1 / 25

Merge Sort

Merge Sort. Merging. The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x 1  x 2  … x m ) and Y(y 1 y 2  … y n ) the resulting list is Z(z 1 z 2  … z m+n ) Example: L 1 = { 3 8 9 } L 2 = { 1 5 7 } merge(L 1 , L 2 ) = { 1 3 5 7 8 9 }.

fiona
Télécharger la présentation

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. Merging • The key to Merge Sort is merging two sorted lists into one, such that if you have two lists X (x1x2…xm) and Y(y1y2…yn) the resulting list is Z(z1z2…zm+n) • Example: L1 = { 3 8 9 } L2 = { 1 5 7 } merge(L1, L2) = { 1 3 5 7 8 9 }

  3. Merging (cont.) X: Y: Result:

  4. Merging (cont.) X: Y: Result:

  5. Merging (cont.) X: Y: Result:

  6. Merging (cont.) X: Y: Result:

  7. Merging (cont.) X: Y: Result:

  8. Merging (cont.) X: Y: Result:

  9. Merging (cont.) X: Y: Result:

  10. Merging (cont.) X: Y: Result:

  11. Merging (cont.) X: Y: Result:

  12. MERGE (A, p, q, r ) • 1.      n1 ← q − p + 12.      n2 ← r − q3.      Create arrays L[1 . . n1 + 1] and R[1 . . n2 + 1]4.      FORi ← 1 TOn15.            DO L[i] ← A[p + i − 1]6.      FORj ← 1 TOn27.            DO R[j] ← A[q + j ]8.      L[n1 + 1] ← ∞9.      R[n2 + 1] ← ∞10.    i ← 111.    j ← 112.    FORk ← pTOr13.         DO IF L[i ] ≤ R[ j]14.                THEN A[k] ← L[i]15.                        i ← i + 116.                ELSE A[k] ← R[j]17.                        j ← j + 1

  13. Merge Sort Algorithm • MERGE-SORT (A, p, r) • 1.     IF p < r                                                    // Check for base case2.         THEN q = FLOOR[(p + r)/2]             // Divide step3.                      MERGE-SORT (A, p, q)            // Divide step 4.                       MERGE-SORT (A, q+1,r)            // Divide step 5.                        MERGE (A, p, q,r)                // Conquer step.

  14. Merge Sort Example

  15. Merge Sort Example

  16. Merge Sort Example

  17. Merge Sort Example

  18. Merge Sort Example

  19. Merge Sort Example Merge

  20. Merge Sort Example Merge

  21. Merge Sort Example Merge

  22. Merge Sort Example Merge

  23. Merge Sort Example

  24. Merge Sort Analysis The Double Memory Merge Sort runs O (N log N) for all cases, because of its Divide and Conquer approach. T(N) = 2T(N/2) + N = O(N logN)

  25. Finally… • There are other variants of Merge Sorts including k-way merge sorting, but the common variant is the Double Memory Merge Sort. Though the running time is O(N logN) and runs much faster than insertion sort and bubble sort, merge sort’s large memory demands makes it not very practical for main memory sorting. • Important things to remember for the Midterm: • Best Case, Average Case, and Worst Case = O(N logN) • Storage Requirement: Double that needed to hold the array to be sorted.

More Related