Download presentation
Presentation is loading. Please wait.
Published byBernice Mills Modified over 5 years ago
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) = ?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.