DivideAndConquer-handouts-2

# DivideAndConquer-handouts-2 - Merge Sort 10:03 PM...

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

Merge Sort 2/19/2006 10:03 PM 1 Divide-and-Conquer 1 Divide-and-Conquer 7 2 9 4 2 4 7 9 7 2 2 7 9 4 4 9 7 7 2 2 9 9 4 4 Divide-and-Conquer 2 Outline and Reading Divide-and-conquer paradigm (§5.2) Review Merge-sort (§4.1.1) Recurrence Equations (§5.2.1) Iterative substitution Recursion trees Guess-and-test The master method Integer Multiplication (§5.2.2)

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

View Full Document
Merge Sort 2/19/2006 10:03 PM 2 Divide-and-Conquer 3 Divide-and-Conquer Divide-and conquer is a general algorithm design paradigm: Divide : divide the input data S in two or more disjoint subsets S 1 , S 2 , … Recur : solve the subproblems recursively Conquer : combine the solutions for S 1 , S 2 , …, into a solution for S The base case for the recursion are subproblems of constant size Analysis can be done using recurrence equations Divide-and-Conquer 4 Merge-Sort Review Merge-sort on an input sequence S with n elements consists of three steps: Divide : partition S into two sequences S 1 and S 2 of about n / 2 elements each Recur : recursively sort S 1 and S 2 Conquer : merge S 1 and S 2 into a unique sorted sequence Algorithm mergeSort ( S, C ) Input sequence S with n elements, comparator C Output sequence S sorted according to C if S.size () > 1 ( S 1 , S 2 ) partition ( S , n /2) mergeSort ( S 1 , C ) mergeSort ( S 2 , C ) S merge ( S 1 , S 2 )
Merge Sort 2/19/2006 10:03 PM 3 Divide-and-Conquer 5 Recurrence Equation Analysis The conquer step of merge-sort consists of merging two sorted sequences, each with n / 2 elements and implemented by means of a doubly linked list, takes at most bn steps, for some constant b . Likewise, the basis case ( n < 2) will take at b most steps. Therefore, if we let T ( n ) denote the running time of merge-sort: We can therefore analyze the running time of merge-sort by finding a closed form solution to the above equation. That is, a solution that has T ( n ) only on the left-hand side. + < = 2 if ) 2 / ( 2 2 if ) ( n bn n T n b n T Divide-and-Conquer 6 Iterative Substitution In the iterative substitution, or “plug-and-chug,” technique, we iteratively apply the recurrence equation to itself and see if we can find a pattern Note that the base case, T(n)=b, occurs when 2 i =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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern