Download presentation
Presentation is loading. Please wait.
1
CSE 2010: Algorithms and Data Structures Running time analysis of mergesort
2
current next current next current next p = 1 r = 8 q = 4 p = 1 r = 4
MERGE-SORT( A, 1, 8 ) current next MERGE-SORT( A, 1, 4 ) p = 1 r = 4 q = 2 current next MERGE-SORT( A, 1, 2 ) p = 1 r = 2 q = 1 current next MERGE-SORT( A, 1, 1) p = 2 r = 2
3
The divide-and-conquer approach
Constant time if problem size is small enough What are the values for a and b in merge sort? a: number of subproblems T(n/b): time to solve subproblem of size n/b. Time to divide the problem into subproblems Time to merge the partial solutions into the solution of the original problems.
4
The divide-and-conquer: worst-case analysis
For merge sort, c is equal to 1. The sum D(n) + C(n) is Theta(n) + Theta(1), which in turn is Theta(n).
5
The divide-and-conquer: worst-case analysis
For merge sort, c is equal to 1. The sum D(n) + C(n) is Theta(n) + Theta(1), which in turn is Theta(n).
6
Time required to solve the problems of size 1, also the time per array element of the divide and combine steps. logn + 1 levels cn Let’s re-rewrite the recurrence equation. Height is logn. So, total time is of order n log n. Each level contributes with a cost of cn. So, total cost is cn long + cn Log n + 1, Base case is log 1 = 0. So, the correct number of levels is log n + 1. cn/2 cn/4 2 x (cn/2) = cn 4 x (cn/4) = cn cn c
7
a >= 1 (must be contant): Number of smaller subproblems
f(n) is the combination time must be positive a >= 1 (must be contant): Number of smaller subproblems Each subproblem is 1/b the size of the original problem, with b > 1.
8
We need to memorize 3 cases
9
Example 1 1st Step: f(n) = (1/2)n^2+n which is polynomial. So, we are okay, and d = 2. a = 1 b = 2 2nd Step: Compare the values of a and b^d.
10
Example 2
11
Example 3
12
Example 4
13
Example 5
14
Example 6 Binary search
15
Example 7
16
Example 8
17
Example 9 The combination time f(n) must be positive.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.