This preview shows pages 1–8. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Divide and Conquer Design and Analysis of Algorithms Andrei Bulatov Algorithms Divide and Conquer 42 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 43 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 44 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 45 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 46 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 47...
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.
 Fall '09
 Bulatov
 Algorithms, Sort

Click to edit the document details