Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II

Similar presentations


Presentation on theme: "Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II"— Presentation transcript:

1 Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II

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

3 Pseudo code for Merge Sort
// MergeSort(A, 1, length[A]) is initial call MergeSort(A, p, r) if p < r // if p >= r then empty or single item q = (p + r)/ // p is the floor of p MergeSort(A, p, q) MergeSort(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 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 Solution for Example 1 Number of levels = ? Cost of each level = ?
Total cost = (# levels)*(cost of each level) = ? T(n) = ?

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

7 Solution for example 2 T(n) = lg(n) T(n) = ?

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

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


Download ppt "Algorithms CSCI 235, Spring 2019 Lecture 7 Recurrences II"

Similar presentations


Ads by Google