Professional Documents
Culture Documents
Recursion is the process which comes into existence when a function calls a copy of
itself to work on a smaller problem. Any function which calls itself is called recursive
function, and such function calls are called recursive calls. Recursion involves several
numbers of recursive calls. However, it is important to impose a termination
condition of recursion. Recursion code is shorter than iterative code however it is
difficult to understand.
Recursion cannot be applied to all the problem, but it is more useful for the tasks
that can be defined in terms of similar subtasks. For Example, recursion may be
applied to sorting, searching, and traversal problems.
Generally, iterative solutions are more efficient than recursion since function call is
always overhead. Any problem that can be solved recursively, can also be solved
iteratively. However, some problems are best suited to be solved by the recursion,
for example, tower of Hanoi, Fibonacci series, factorial finding, etc.
1. #include <stdio.h>
2. int fact (int);
3. int main()
4. {
5. int a,f;
6. printf("Enter the number whose factorial you want to calculate?");
7. scanf("%d",&a); 5
8. f = fact(a);
9. printf("factorial = %d",f);
10. }
11. int fact(int n)
12. {
13. if (n==0)
14. {
15. return 1;
16. }
17.
18.
19. else
20. {
21. return (n*fact(n-1));
22. }
23. }
We can understand the above program of the recursive method call by the figure given
below:
Let's see an example to find the nth term of the Fibonacci series.
1. #include<stdio.h>
2. int fibonacci(int);
3. void main ()
4. {
5. int n,f,i;
6. printf("Enter the value of n?");
7. scanf("%d",&n);
8. For(i=0;i<n;i++)
9. {
10. f = fibonacci(i);
11. printf("Fibonacci(%d)=%d",i,f);
12. }
13. }
14. int fibonacci (int n)
15. {
16. if (n==0)
17. {
18. return 0;
19. }
20. else if (n == 1)
21. {
22. return 1;
23. }
24. else
25. {
26. return fibonacci(n-1)+fibonacci(n-2);
27. }
28. }
Output
Ackermann Function
In computability theory, the Ackermann function, named after Wilhelm
Ackermann, is one of the simplest and earliest-discovered examples of a
total computable function that is not primitive recursive. All primitive recursive
functions are total and computable, but the Ackermann function illustrates
that not all total computable functions are primitive recursive. Refer this for
more.
It’s a function with two arguments each of which can be assigned any non-
negative integer.
The intuitive reason for why it is not primitive recursive is that it is recursing on more than
one parameters, the primitive recursive functions are defined by functions recursing on
only one parameter.
3. At this point, elements are already sorted. Finally, elements are combined
to form a sorted array.
1. A pointer is fixed at the pivot element. The pivot element is compared with
the elements beginning from the first index.
Comparison of
pivot element with element beginning from the first index
2. If the element is greater than the pivot element, a second pointer is set for
that element. If
the element is greater than the pivot element, a second pointer is set for
that element.
3. Now, pivot is compared with other elements. If an element smaller than the
pivot element is reached, the smaller element is swapped with the greater
element found earlier.
Pivot is compared
with other elements.
4. Again, the process is repeated to set the next greater element as the
second pointer. And, swap it with another smaller element.
The process is
repeated to set the next greater element as the second pointer.
3. Divide Subarrays
Pivot elements are again chosen for the left and the right sub-parts
separately. And, step 2 is repeated.
storeIndex++
return storeIndex + 1
You can understand the working of quicksort algorithm with the help of the
illustrations below.
Sorting the elements on the left of pivot using recursionSorting the elements on the right of pivot
using recursion
// Quick sort in C
#include <stdio.h>