« Theoretical analysis for time taken by recursion algorithm

Time complexity analysis for Factorial using recursion

recursion_time_complexity_analysis.jpg

T(n) = k1 + k2 + T(n-1)

=> T(n) = k + T(n-1)

=> T(n) = T(n-1) + k


T(n) = T(n-1) + k

T(n-1) = T(n-2) + k

T(n-2) = T(n-3) + k

...

T(1) = T(0) + k;

T(0) = k;

Add all the above mentioned

recurrance_relation_solution.jpg

T(n) = k(n+1)

T(n) = kn + k

T(n) = kn

T(n) = O(n)

So, for factorial time complexity will be O(n)

Time complexity analysis for Binary search using recursion

binary_search.jpg

T(n) = k + T(n/2)

T(n/2) = k + T(n/4)

T(n/4) = k + T(n/8)

...

T(1) = k

Adding all above gives

T(n) = kx

x = log(n)

T(n) => O(log(n))

recurrance_relation_binary_search.jpg

log(n) is a very fast algorithm

for 106 elements we only need to perform 20 searches.

how_fast_logn_is.jpg