Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout << n << " "; repeat( --n );

Similar presentations


Presentation on theme: "Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout << n << " "; repeat( --n );"— Presentation transcript:

1 Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout << n << " "; repeat( --n ); }

2 Recursion contd. Simple recursive routines follow a standard pattern. Typically there is a base case ( or cases ) that is tested for, upon entry to the function. Then there is a general recursive case in which one of the variables, often an integer, is passed as an argument in such a way as to ultimately lead to the base case.

3 Recursion contd. When writing a recursive function, there are two rules that need to be adhered to. – Every recursive function must have a base case, where the result is known – When a function invokes itself, the value is defined in terms of previously defined function values, and the value used for testing for the base case must change.

4 Recursion contd. void repeat( int n ) { if( n<0 ) { cout << "END" << endl; return; } else { cout << n << " "; repeat( --n ); }

5 Recursion Example int sum( int n ) { if (n <= 1 ) return n;// base case else return (n + sum( n -1 ));// current value is defined in terms of // a different value of the parameter } sum(1)1 sum(2)2 + sum(1)2 + 1 sum(3)3 + sum(2)3 + 2 + 1

6 Factorial Example Let’s find the factorial of n where n is a non-negative integer. – Product of integers from 1 to n – If n = 0, then n! is 1 0! = 1 n! = n(n-1)… 3.2.1 for n > 0 int factorial( int n ) { if (n <= 1 ) return 1; else return ( n * factorial(n-1 ) ); } What is the Big-Oh?

7 Iterative Version of the Factorial Example int factorial( int n ) { int value = 1; for ( int i=2, i<=n, i++ ) value *= i; return value; } What is the Big-Oh for this?

8 Pros and Cons of Recursion Disadvantages: – Slow – time is taken for function call – Uses more memory – requires space for stack frame Advantages: – When it is a simple, elegant solution that is easy to understand – When the number of recursive calls is a fraction of n

9 When to Use Recursion 1.Iterative functions are typically faster than their recursive counterparts. So, if speed is an issue, you would normally use iteration. 2.If the stack limit is too constraining then you will prefer iteration over recursion. 3.Some procedures are very naturally programmed recursively, and all but unmanageable iteratively. Here, then, the choice is clear.

10 Example of When to Use Recursion Algorithm Power( x, n ) Input: base x, exponent n where n  0 Output: value of x n if n = 0 then return 1 if n is odd then y  Power(x,(n-1)/2) return x * y * y else y  Power(x,n/2) return y * y 1if n = 0 power(x,n)x * power(x,(n-1)/2) 2 if n>0 is odd power(x,n/2) 2 if n>0 is even


Download ppt "Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout << n << " "; repeat( --n );"

Similar presentations


Ads by Google