1 / 44

Data Structures and Algorithms

Data Structures and Algorithms . Merge Sort Hairong Zhao http://web.njit.edu/~hz2 New Jersey Institute of Technology. This Lecture. Divide and Conquer Merge Sort. Divide and Conquer. Base case: the problem is small enough, solve directly

cara
Download Presentation

Data Structures and Algorithms

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. Data Structures and Algorithms Merge Sort Hairong Zhao http://web.njit.edu/~hz2 New Jersey Institute of Technology

  2. This Lecture • Divide and Conquer • Merge Sort

  3. Divide and Conquer • Base case: the problem is small enough, solve directly • Divide the problem into two or more similar and smaller subproblems • Recursively solve the subproblems • Combine solutions to the subproblems

  4. Divide and Conquer - Sort Problem: • Input: A[n] – unsorted array of n ≥1 integers. • Output:A[n] – sorted in non-decreasing order

  5. Divide and Conquer - Sort • Base case • single element (n=1), return • DivideA into twosubarrays: FirstPart, SecondPart • Two Subproblems: • sort the FirstPart • sort the SecondPart • Recursively • sort FirstPart • sort SecondPart • Combinesorted FirstPart and sorted second part

  6. Merge Merge Sort: Idea Divide into two halves A: FirstPart SecondPart Recursively sort SecondPart FirstPart A is sorted!

  7. Merge Sort: Algorithm • Merge-Sort(A, n) • if n=1 return • else • n1← n2← n/2 • create array L[n1], R[n2] • for i ← 0 to n1-1 do L[i] ← A[i] • for j ← 0 to n2-1 do R[j] ← A[n1+j] • Merge-Sort(L, n1) • Merge-Sort(R, n2) • Merge(A, L, n1, R, n2 ) Space: n Time: n Recursive Call

  8. Merge-Sort: Merge Sorted A: merge Sorted Sorted R: L:

  9. 1 2 6 8 3 4 5 7 Merge-Sort: Merge Example A: L: R:

  10. Merge-Sort: Merge Example A: 3 1 5 15 28 10 14 k=0 R: L: 3 1 15 2 28 6 30 8 3 6 4 10 14 5 7 22 i=0 j=0

  11. Merge-Sort: Merge Example A: 2 1 5 15 28 30 6 10 14 k=1 R: L: 3 1 2 5 15 6 28 8 6 3 10 4 5 14 22 7 j=0 i=1

  12. Merge-Sort: Merge Example A: 3 1 2 15 28 30 6 10 14 k=2 R: L: 1 2 6 8 6 3 4 10 5 14 7 22 j=0 i=2

  13. Merge-Sort: Merge Example A: 1 2 3 6 10 14 4 k=3 R: L: 1 2 6 8 3 6 10 4 14 5 7 22 j=1 i=2

  14. Merge-Sort: Merge Example A: 1 2 3 4 6 10 14 5 k=4 R: L: 1 2 6 8 3 6 10 4 14 5 7 22 i=2 j=2

  15. Merge-Sort: Merge Example A: 6 1 2 3 4 5 6 10 14 k=5 R: L: 1 2 6 8 6 3 4 10 5 14 7 22 i=2 j=3

  16. Merge-Sort: Merge Example A: 7 1 2 3 4 5 6 14 k=6 R: L: 1 2 6 8 6 3 4 10 14 5 7 22 j=3 i=3

  17. Merge-Sort: Merge Example A: 8 1 2 3 4 5 5 7 14 k=7 R: L: 1 3 5 2 6 15 28 8 6 3 10 4 5 14 22 7 i=3 j=4

  18. Merge-Sort: Merge Example A: 1 2 3 4 5 6 7 8 k=8 R: L: 3 1 2 5 15 6 8 28 3 6 10 4 14 5 22 7 j=4 i=4

  19. merge(A,L,n1,R,n2) i ← j ← 0 for k ← 0 to n1+n2-1 if i < n1 if j = n2 or L[i] ≤ R[j] A[k] ← L[i] i ← i + 1 else if j < n2 A[k] ← R[j] j ← j + 1 Number of iterations: (n1+n2) Total time: c(n1+n2) for some c

  20. Merge-Sort Execution Example Divide 3 7 5 1 6 2 8 4 6 2 8 4 3 7 5 1

  21. Merge-Sort Execution Example , divide Recursive call 3 7 5 1 8 4 6 2 6 2 8 4

  22. Merge-Sort Execution Example , divide Recursive call 3 7 5 1 8 4 2 6 6 2

  23. Merge-Sort Execution Example , base case Recursive call 3 7 5 1 8 4 2 6

  24. Merge-Sort Execution Example Recursive call return 3 7 5 1 8 4 2 6

  25. Merge-Sort Execution Example , base case Recursive call 3 7 5 1 8 4 6 2

  26. Merge-Sort Execution Example Recursive call return 3 7 5 1 8 4 2 6

  27. Merge-Sort Execution Example Merge 3 7 5 1 8 4 2 6

  28. Merge-Sort Execution Example Recursive call return 3 7 5 1 8 4 2 6

  29. Merge-Sort Execution Example , divide Recursive call 3 7 5 1 2 6 4 8 8 4

  30. Merge-Sort Execution Example Recursive call, base case 3 7 5 1 2 6 4 8

  31. Merge-Sort Execution Example Recursive call return 3 7 5 1 2 6 4 8

  32. Merge-Sort Execution Example Recursive call, base case 2 6 8 4

  33. Merge-Sort Execution Example Recursive call return 3 7 5 1 2 6 4 8

  34. Merge-Sort Execution Example merge 3 7 5 1 2 6 4 8

  35. Merge-Sort Execution Example Recursive call return 3 7 5 1 4 8 2 6

  36. Merge-Sort Execution Example merge 3 7 5 1 2 4 6 8

  37. Merge-Sort Execution Example Recursive call return 2 4 6 8 3 7 5 1

  38. Merge-Sort Execution Example Recursive call 2 4 6 8 3 7 5 1

  39. Merge-Sort Execution Example 2 4 6 8 1 3 5 7

  40. Merge-Sort Execution Example Recursive call return 2 4 6 8 1 3 5 7

  41. Merge-Sort Execution Example merge 1 2 3 4 5 6 7 8

  42. Merge-Sort Analysis • Time, divide n n 2 × n/2 = n n/2 n/2 log n levels 4 × n/4 = n n/4 n/4 n/4 n/4 n/2 × 2 = n Total time for divide: n log n

  43. Merge-Sort Analysis • Time, merging cn n 2 × cn/2 = n n/2 n/2 log n levels 4 × cn/4 = n n/4 n/4 n/4 n/4 n/2 × 2c = n Total time for merging: cn log n • Total running time: order of nlogn • Total Space: order of n

  44. Homework • What is the running time of Merge-Sort if the array is already sorted? What is the best case running time of Merge-Sort? Justify the answer.

More Related