1 / 80

Lecture 6

Lecture 6. Merge Sort: Algorithm . Merge-Sort (A, left, right) if left ≥ right return else middle ← ( left+right )/2  Merge-Sort (A, left, middle) Merge-Sort (A, middle+1, right) Merge (A, left, middle, right). Recursive Call. Merge-Sort: Merge. Sorted. A:. merge.

otylia
Télécharger la présentation

Lecture 6

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. Lecture 6

  2. Merge Sort: Algorithm • Merge-Sort(A, left, right) • if left ≥ right return • else • middle ←(left+right)/2 • Merge-Sort(A, left, middle) • Merge-Sort(A, middle+1, right) • Merge(A, left, middle, right) Recursive Call

  3. Merge-Sort: Merge Sorted A: merge Sorted SecondPart SortedFirstPart A: A[right] A[left] A[middle]

  4. 5 15 28 30 6 10 14 5 2 3 7 8 1 4 5 6 R: L: 3 5 15 28 6 10 14 22 Temporary Arrays Merge-Sort: Merge Example A:

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

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

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

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

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

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

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

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

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

  14. Merge(A, left, middle, right) • 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 = n1+n2 Space: n Time : cn for some constant c

  15. Merge-Sort(A, 0, 7) Divide A: 3 7 5 1 6 2 8 4 6 2 8 4 3 7 5 1

  16. Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 3) A: 3 7 5 1 8 4 6 2 6 2 8 4

  17. Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 0, 1) A: 3 7 5 1 8 4 2 6 6 2

  18. Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 0, 0) A: 3 7 5 1 8 4 2 6

  19. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 0), return A: 3 7 5 1 8 4 2 6

  20. Merge-Sort(A, 0, 7) , base case Merge-Sort(A, 1, 1) A: 3 7 5 1 8 4 6 2

  21. Merge-Sort(A, 0, 7) Merge-Sort(A, 1, 1), return A: 3 7 5 1 8 4 2 6

  22. Merge-Sort(A, 0, 7) Merge(A, 0, 0, 1) A: 3 7 5 1 8 4 2 6

  23. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 1), return A: 3 7 5 1 8 4 2 6

  24. Merge-Sort(A, 0, 7) , divide Merge-Sort(A, 2, 3) A: 3 7 5 1 2 6 4 8 8 4

  25. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2), base case A: 3 7 5 1 2 6 4 8

  26. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 2),return A: 3 7 5 1 2 6 4 8

  27. Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), base case A: 2 6 8 4

  28. Merge-Sort(A, 0, 7) Merge-Sort(A, 3, 3), return A: 3 7 5 1 2 6 4 8

  29. Merge-Sort(A, 0, 7) Merge(A, 2, 2, 3) A: 3 7 5 1 2 6 4 8

  30. Merge-Sort(A, 0, 7) Merge-Sort(A, 2, 3), return A: 3 7 5 1 4 8 2 6

  31. Merge-Sort(A, 0, 7) Merge(A, 0, 1, 3) A: 3 7 5 1 2 4 6 8

  32. Merge-Sort(A, 0, 7) Merge-Sort(A, 0, 3), return A: 2 4 6 8 3 7 5 1

  33. Merge-Sort(A, 0, 7) Merge-Sort(A, 4, 7) A: 2 4 6 8 3 7 5 1

  34. Merge-Sort(A, 0, 7) Merge (A, 4, 5, 7) A: 2 4 6 8 1 3 5 7

  35. Merge-Sort(A, 0, 7) Merge-Sort(A, 4, 7), return A: 2 4 6 8 1 3 5 7

  36. 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. Merge-Sort Analysis cn n 2 × cn/2 = cn n/2 n/2 log n levels 4 × cn/4 = cn n/4 n/4 n/4 n/4 n/2 × 2c = cn 2 2 2 Total: cn log n • Total running time:(nlogn) • Total Space: (n)

  38. 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.

  39. Merge Sort Analysis • Suppose n is a power of two, so that we always split into even halves. • For n = 1 the time to merge sort is1 otherwise • The time to merge sort n numbers is equal to the time to do two recursive merge sorts of size n/2, plus the time to merge, which is linear. T(1) = 1 T(n) = 2T(n/2) + n • Solve this recurrence to find out running time.

  40. Solution by Telescoping Sum T(n) = 2T(n/2) + n Divide the recurrence relation through by n. T(n) / n = T(n/2) / n/2 + 1 This equation is valid for any n that is a power of 2. So T(n/2) / n/2 = T(n/4) / n/4 + 1 T(n/4) / n/4 = T(n/8) / n/8 + 1 ….. T(2) / 2 = T(1) / 1 + 1 Add both sides of all the above equations T(n) / n + T(n/2) / n/2 + T(n/4) / n/4 + ….+ T(2) / 2 = T(n/2) / 2 + 1 + T(n/4) / n/4 + 1 + T(n/8) / n/8 + 1 +…..+ T(1) / 1 + 1

  41. Solution by Telescoping Sum T(n) / n + T(n/2) / n/2 + T(n/4) / n/4 + ….+ T(2) / 2 = T(n/2) / 2 + 1 + T(n/4) / n/4 + 1 + T(n/8) / n/8 + 1 +…..+ T(1) / 1 + 1 • All the terms other than T(n) / n on right hand side cancel the terms on left hand side. • Since n is a power of 2, these equations add up to lgn (Tree has lgn levels). T(n) / n = T(1) / 1 + lgn T(n) = n + nlgn T(n) = O(nlgn)

  42. Solution by Substitution Method T(n) = 2 T(n/2) + n • Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n) = 4T (n/4) + 2n • Again by substituting n/4, we can see 4T(n/4) = 4(2T(n/8)) + (n/4) = 8T(n/8) + n And T(n) = 8T(n/8) + 3n • Continuing in this manner, we obtain T(n) = 2k T(n/2k) + k.n Using k = lgn T(n) = nT(1) + nlgn = nlgn + n T(n) = O(nlgn)

  43. Problem with Merge Sort • Merging two sorted lists requires linear extra memory. • Additional work spent copying to the temporary array and back through out the algorithm. • This problem slows down the sort considerably.

  44. Overview • Divide and Conquer • Merge Sort • Quick Sort

  45. Quick Sort • Divide: • Pick any element p as the pivot, e.g, the first element • Partition the remaining elements into FirstPart,which contains all elements< p SecondPart, which contains all elements ≥ p • Recursively sort the FirstPart and SecondPart • Combine: no work is necessary since sorting is done in place

  46. Partition Recursive call p p p p ≤ x p ≤ x Sorted FirstPart Sorted SecondPart x < p Quick Sort A: pivot FirstPart SecondPart x < p Sorted

  47. Quick Sort Quick-Sort(A, left, right) ifleft ≥ right return else middle ← Partition(A, left, right) Quick-Sort(A, left, middle–1 ) Quick-Sort(A, middle+1, right) end if

  48. p x < p p p ≤ x x < p p ≤ x p Partition A: A: A: p

  49. Partition Example A: 4 8 6 3 5 1 7 2

  50. Partition Example i=0 A: 4 8 6 3 5 1 7 2 j=1

More Related