2. • Any function which call it self called a Recursive function.
• A recursive function solve a problem by calling a copy of itself to work on a
smaller problem called Recursive function.
• In Recursion it is important to insure the Recursion terminates.
• Recursive code are generally shorter and easier to write than Iterative function.
• Recursion is most useful for a task which can be define in terms of similar
subtask.
&7Ciph
er
3. • A Recursive function perform a task by calling itself to perform a subtask and
at some point the function encounter a subtask that can be performed without
calling itself.
• The case where the function do not recur is called BASE CASE.
Example:
Program to find factorial of any number n
n! = 1 if n == 0
n! = n * (n-a)! if n > 0
&7Ciph
er
4. • Iterative solution :
int iterativeFactorialMethod(int n){
int factorial = 1;
if (n <= 0)
{
cout << "Factorial of number is : " << factorial << endl;
return factorial;
}
else
{
for (int i = 1; i <= n; i++)
{
factorial = factorial * i;
}
&7Ciph
er
5. • Recursive solution :
int recursiveFactorialMethod(int n){
int factorial = 1;
if (n <= 0)
{
cout << "Factorial of number is : " << factorial << endl;
return factorial;
}
else
{
factorial = n * recursiveFactorialMethod(n-1);
}
cout << "Factorial of number is : " << factorial << endl;
&7Ciph
er
6.
7. • Iteration terminates when a condition is proven to be false.
• Each iteration do not require extra spaces.
• Ab infinite loop could loop forever since there is no extra spaces being
created.
• Iterative solution to a problem may not the obvious as recursive solution.
&7Ciph
er
8. • A recursive approach mirror the problem which we are trying to solve.
• A Recursive approach make it simpler to solve a problem that may not have
the obvious answer or solution but recursion adds overheads for each
recursive call.
• Which means it needs more space on stack.
• Every recursive solution gets memory in stack which means if we will solve it
with a large input and we have a low memory in our stack than at run time we
will encounter stack-over-flow situation.
&7Ciph
er
9. • Recursion get terminated when it reach to the base case.
• Each recursion call need extra space on stack frame.
• If we encounter infinite recursion the program may run out of memory and that
result stack over flow.
• Solution of some problem can easier to formulate with recursion.
&7Ciph
er
10. • Recursive algorithm have two type of case.
1. Recursive case
2. Base case
• Every recursive algorithm must terminates on his Base case.
• Generally Iterative solution are more efficient than Recursive solution.
• For some problem there are no obvious Iterative solution possible.
• Some problems are best suited for Recursive algorithm and vice-versa.
&7Ciph
er
11. • Fibonacci series
• Merge sort
• Quick sort
• Tower of Hanoi
• Divide and Conquer Algorithm
• Dynamic Programming
• Graph traversal (DFS and BFS)
• Backtracking Algorithm
&7Ciph
er