{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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)

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}