Merge sort

599 views

Published on

Published in: Education, Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
599
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
40
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Merge sort

  1. 1. Merge Sort
  2. 2. Merge Sort: Idea Divide into two halves A FirstPart SecondPart Recursively sort SecondPart FirstPart Merge A is sorted! 2
  3. 3. Merge Sort: Algorithm Merge-Sort (A, left, right) if left ≥ right return else middle ← b(left+right)/2 Merge-Sort(A, left, middle) Recursive Call Merge-Sort(A, middle+1, right) Merge(A, left, middle, right) 3
  4. 4. Merge-Sort: Merge Sorted A: merge Sorted FirstPart Sorted SecondPart A: A[left] A[middle] A[right] 4
  5. 5. Merge-Sort: Merge Example A: 2 5 3 5 7 8 1 15 28 30 4 6 5 6 10 14 R: L: 3 5 15 28 6 10 14 22 Temporary Arrays 5
  6. 6. Merge-Sort: Merge Example A: 3 1 5 15 28 30 6 10 14 k=0 R: L: 2 3 i=0 15 28 30 3 7 8 1 6 10 14 22 4 5 6 j=0 6
  7. 7. Merge-Sort: Merge Example A: 1 2 5 15 28 30 6 10 14 k=1 R: L: 2 3 i=0 3 5 15 28 7 8 1 6 10 14 22 4 5 6 j=1 7
  8. 8. Merge-Sort: Merge Example A: 1 2 3 15 28 30 6 10 14 k=2 R: L: 2 3 i=1 7 8 1 6 10 14 22 4 5 6 j=1 8
  9. 9. Merge-Sort: Merge Example A: 1 2 3 4 6 10 14 k=3 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=1 9
  10. 10. Merge-Sort: Merge Example A: 1 2 3 4 5 6 10 14 k=4 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=2 10
  11. 11. Merge-Sort: Merge Example A: 1 2 3 4 5 6 10 14 k=5 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=3 11
  12. 12. Merge-Sort: Merge Example A: 1 2 3 4 5 7 6 14 k=6 R: L: 2 3 7 i=2 8 1 6 10 14 22 4 5 6 j=4 12
  13. 13. Merge-Sort: Merge Example A: 1 2 3 4 5 6 7 8 14 k=7 R: L: 2 3 3 5 15 28 7 8 i=3 1 6 10 14 22 4 5 6 j=4 13
  14. 14. Merge-Sort: Merge Example A: 1 2 3 4 5 6 7 8 k=8 R: L: 2 3 3 5 15 28 7 8 1 6 i=4 10 14 22 4 5 6 j=4 14
  15. 15. Merge(A, left, middle, right) 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. n1 ← middle – left + 1 n2 ← right – middle create array L[n1], R[n2] for i ← 0 to n1-1 do L[i] ← A[left +i] for j ← 0 to n2-1 do R[j] ← A[middle+j] k ← i ← j ← 0 while i < n1 & j < n2 if L[i] < R[j] A[k++] ← L[i++] else A[k++] ← R[j++] while i < n1 A[k++] ← L[i++] while j < n2 A[k++] ← R[j++] n = n +n 1 2 Space: n Time : cn for some constant c 15
  16. 16. Merge-Sort(A, 0, 7) Divide A: 6 2 8 4 33 7 7 55 11 16
  17. 17. Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 3) 3 A: 6 2 88 7 5 1 44 17
  18. 18. Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 1) 3 A: 8 6 7 5 1 4 2 18
  19. 19. Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 0, 0) 3 A: 8 7 5 1 4 2 6 19
  20. 20. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 0), return 3 A: 8 6 7 5 1 4 2 20
  21. 21. Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 1, 1) 3 A: 8 7 5 1 4 6 2 21
  22. 22. Merge-Sort(A, 0, 7) Merge-Sort(A, 1, 1), return 3 A: 8 6 7 5 1 4 2 22
  23. 23. Merge-Sort(A, 0, 7) Merge(A, 0, 0, 1) 3 A: 8 2 7 5 1 4 6 23
  24. 24. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 1), return 3 A: 2 6 8 7 5 1 4 24
  25. 25. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 3) , divide 3 A: 2 7 5 1 6 8 4 25
  26. 26. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2), base case 3 A: 2 7 5 1 6 4 8 26
  27. 27. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2), return 3 A: 2 7 5 1 6 8 4 27
  28. 28. Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), base case A: 2 6 8 4 28
  29. 29. Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), return 3 A: 2 7 5 1 6 8 4 29
  30. 30. Merge-Sort(A, 0, 7) Merge(A, 2, 2, 3) 3 A: 2 7 5 1 6 4 8 30
  31. 31. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 3), return 3 A: 2 6 4 7 5 1 8 31
  32. 32. Merge-Sort(A, 0, 7) Merge(A, 0, 1, 3) 3 A: 2 4 6 7 5 1 8 32
  33. 33. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 3), return A: 2 4 6 8 3 7 5 1 33
  34. 34. Merge-Sort(A, 0, 7) Merge-Sort(A, 4, 7) A: 2 4 6 8 3 7 5 1 34
  35. 35. Merge-Sort(A, 0, 7) Merge (A, 4, 5, 7) A: 2 4 6 8 1 3 5 7 35
  36. 36. Merge-Sort(A, 0, 7) Merge-Sort(A, 4, 7), return A: 2 4 6 8 1 3 5 7 36
  37. 37. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 7), done! Merge(A, 0, 3, 7) A: 1 2 3 4 5 6 7 8 37
  38. 38. Merge-Sort Analysis cn n n/4 2 2 × cn/2 = cn n/2 n/2 n/4 2 n/4 n/4 2 4 × cn/4 = cn log n levels n/2 × 2c = cn Total: cn log n • Total running time: (nlogn) • Total Space: (n) 38
  39. 39. • Let T(n) be the time taken by the merge sort to sort an array of n elements. • Divide the T into U and V takes linear time. • Hence, T ( n) T n 2 T n 2 g ( n) where g ( n) ( n) This recurrenc e w hic h becomes , T(n) n 2T 2 g ( n)
  40. 40. n T(n) 2T g ( n) 2 a 2 b 2 f(n) g(n) n log b a n log 2 2 n f ( n) ( n) case 2 of Master's theorm applied then T(n) (n log b a log n) (n log n)
  41. 41. Merge-Sort Summary Approach: divide and conquer Time – Most of the work is in the merging – Total time: (n log n) Space: – (n), more space than other sorts. 41

×