Recursion and Iteration
Recursion and Iteration
Iteration
What is Recursion?
The process in which a function calls itself directly or indirectly is called
recursion and the corresponding function is called a recursive function.
Using a recursive algorithm, certain problems can be solved quite easily.
Examples of such problems are Towers of Hanoi (TOH),
Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc. A
recursive function solves a particular problem by calling a copy of itself
and solving smaller subproblems of the original problems. Many more
recursive calls can be generated as and when required. It is essential to
know that we should provide a certain case in order to terminate this
recursion process. So we can say that every time the function calls itself
with a simpler version of the original problem.
Recursion is mainly of two types depending on whether a function calls
itself from within itself or more than one function call one another
mutually. The first one is called direct recursion and another one is
called indirect recursion. Thus, the two types of recursion are:
From the above diagram fun(A) is calling for fun(B), fun(B) is calling for
fun(C) and fun(C) is calling for fun(A) and thus it makes a cycle.
Need of Recursion
Step 1 - Define a base case: Identify the simplest case for which the solution is known or trivial.
This is the stopping condition for the recursion, as it prevents the function from infinitely calling
itself.
Step 2 - Define a recursive case: Define the problem in terms of smaller subproblems. Break the
problem down into smaller versions of itself, and call the function recursively to solve each
subproblem.
Step 3 - Ensure the recursion terminates: Make sure that the recursive function eventually
reaches the base case, and does not enter an infinite loop.
Step 4 - Combine the solutions: Combine the solutions of the subproblems to solve the original
problem.
What is Iteration?
When there is a repetition or loop, it is known as iterative. In Iteration,
we prefer loops to perform the group of instructions repetitively until
the state of the iteration becomes wrong.
1. Iterative Implementation
2. Recursive Implementation
1. Iterative Implementation of Binary Search in C
Create a function that takes an array, left index, right index, and the key to be
searched.
Use a loop to iterate while the subarray has elements i.e. left < right.
Calculate the midpoint mid.
Compare the key with the middle element i.e. arr[mid]
If the key matches the middle element, return mid.
If the key is greater, adjust the left index to search the right subarray by
making left = mid + 1.
If the key is smaller, adjust the right index to search the left subarray by
making right = mid – 1.
If the key is not found, return -1.
// C Program to implement binary search using iteration
#include <stdio.h>
// Loop will run till left > right. It means that there
// are no elements to consider in the given subarray
while (left <= right) {
// Element to be searched
int key = 23;
// element to be searched
int key = 23;