algorithm - Apply master theorem on T(n) = T(n/2) + n -
I was just trying my hand at the master theorem and when I try to evaluate t (n) = t Using (N / 2) + An Master Theorem, the answer is O (N) evaluating.
But just go through the code given below:
Fun (n) {if (n == 1) Return; For (int i = 1; i & lt; = n; i ++) {printf ("*"); } Fun (N / 2); }
The recurring equation for the above code is T (n) = t (n / 2) + n, so the complexity of time for the above program should be o (n).
But if you think that logically, the number of times the program is running: n + n / 2 + n / 4 + n / 8 + ...... = nlogn Therefore, logical As per the above program, the complexity of time should be O (NLONP).
I am very confused now. Can someone help me, where is this happening to me?
No, evaluates section 2n
n + n / 2 + n / 4 + n / 8 + ...... = 2n
But if you have T (n) = 2 t (n / 2) + n, then It will be O (n log n)
Comments
Post a Comment