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

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov 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) ombine solutions to subproblems (combine) 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 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 [p], , A[q] and A[q+1], , A[r] are ordered 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 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...
View Full Document

This note was uploaded on 11/11/2009 for the course CS 405/705 taught by Professor Bulatov during the Fall '09 term at Simon Fraser.

Page1 / 20

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

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online