Solve this problem first.
What is the time complexity of the recursive function?
1 2 3 4 |
|
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
By induction, clearly f u n c ( n ) = n for all n . (The base case is n ≤ 0 ; the inductive case uses the inductive hypothesis twice to get f u n c ( f u n c ( n − 1 ) ) = f u n c ( n − 1 ) = n − 1 .)
Let T ( n ) be the running time of the function with argument n . We obtain the recurrence T ( n ) = 2 T ( n − 1 ) + O ( 1 ) , since f u n c ( f u n c ( n − 1 ) ) evaluates to f u n c ( n − 1 ) in T ( n − 1 ) time, and to n − 1 in further T ( n − 1 ) time, before adding a constant time for the addition. Thus we can use induction again to see that T ( n ) = O ( 2 n ) .