Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II.

Similar presentations


Presentation on theme: "1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II."— Presentation transcript:

1 1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II

2 2 Recall Merge Sort Divide in 1/2 repeatedly merge in sorted order

3 3 Pseudo code for Merge Sort {merge-sort(A, 1, length[A]) is initial call} {merge-sort(A, p, r)} if p = r then empty or single item} q  (p + r)/2 { p is the floor of p } merge-sort(A, p, q) merge-sort(A, q+1, r) merge (A, p, q, r) Size of each subproblem = ? Number of subproblems = ? Running time of merge = ? Equation for Merge Sort: ?

4 4 Example 1 Working toward merge-sort A simpler equation: T(n) = T(n/2) + 1 We will draw the recursion tree for this in class.

5 5 Solution for Example 1 Total cost = (# levels)*(cost of each level) = ? T(n) = ? Number of levels = ? Cost of each level = ?

6 6 Example 2 (working toward merge-sort) Another equation: T(n) = 2T(n/2) + 1

7 7 Solution for example 2 T(n) = 1 + 2 + 4 + 8 +... + 2 lg(n) T(n) = ?

8 8 Example 3 merge-sort merge-sort equation: T(n) = 2T(n/2) + n

9 9 Solving Recursion tree for Merge-Sort Total cost = (# levels)*(cost of each level) = ? T(n) =  Number of levels = ? Cost of each level = ?


Download ppt "1 Algorithms CSCI 235, Fall 2015 Lecture 7 Recurrences II."

Similar presentations


Ads by Google