Advertisement
naren_paste

Recursive_3_B

Dec 6th, 2023
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.55 KB | Source Code | 0 0
  1. T(n) = 2T(n/2) + n
  2.        T(n)---------n
  3.        /    \
  4.  T(n/2)       T(n/2)-------2(n/2)
  5.    / \          /   \
  6. T(n/4) T(n/4) T(n/4) T(n/4) ------4(n/4)
  7.   / \    ...   ...   ...
  8. ...  ...
  9. Now, let's write the expression for the total cost:
  10. LHS
  11. n/2^(k)=1        The depth of the tree is logn base 2.
  12. k=log n base 2
  13. let's calculate the total work done by summing up the work at each level
  14. T(n)=n+n/2+n/4+n/8.......+n/2^k
  15. T(n)=n(1+1/2+1/4+.........1/2 ^logn base 2)
  16. by geometric progression  sum of the series is 1/1-r=2
  17. T(n)=n(2)
  18. time complexity is  O(n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement