{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

04 - Divide and Conquer Design and Analysis of Algorithms...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Algorithms – Divide and Conquer 4-2 Divide and Conquer, MergeSort Recursive algorithms : Call themselves on subproblem Divide and Conquer algorithms: Split a problem into subproblems (divide) Solve subproblems recursively (conquer) Combine solutions to subproblems (combine) MergeSort Divide: Split a given sequence into halves Conquer: By calling itself sort the two halves Combine: Merge the two sorted arrays into one
Background image of page 2
Algorithms – Divide and Conquer 4-3 MergeSort 5 2 4 7 1 3 2 6 1 3 2 6 5 2 4 7 5 2 4 7 2 6 3 1 2 7 3 6 5 4 1 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Algorithms – Divide and Conquer 4-4 MergeSort MergeSort(A,p,r) Input: array A, positions p,r Output : array A such that entries A[p],…,A[r] are sorted Method : if p<r then do set q:= (p+r)/2 MergeSort(A,p,q) MergeSort(A,q+1,r) Merge(A,p,q,r) endif
Background image of page 4
Algorithms – Divide and Conquer 4-5 Merge The Merge procedure is applied to array A and three positions p, q, r in this array Assume p q < r A[p], …, A[q] and A[q+1], …, A[r] are ordered Outputs ordered sequence in positions A[p], …, A[r] This sequence is generated by comparing the two elements on the top of subarrays and moving the smaller one
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Algorithms – Divide and Conquer 4-6 MergeSort: Running Time The running time of Merge when applied to two arrays of total size n is Θ (n) The running time, T(n), of MergeSort is T(n) = Cn + T(n/2) + T(n/2) + Dn If n = 1 then T(1) = C divide recursion Merge
Background image of page 6
Algorithms – Divide and Conquer 4-7 MergeSort: Soundness Recursion tree There are nodes on level i Each node requires work i 2 i Dn Cn 2 Total work on each level: (C+D)n There are log n levels Theorem The running time of MergeSort is Θ (n log n)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}