Recursive Adventures: Unleashing the Power of Recursion vs. Looping

Varsha C Bendre
4 min readNov 5, 2022

Diving into the Depths of Code Efficiency and Problem Solving with Recursion and Looping in Programming

1. Introduction

The process of Programming is creating a set of instructions to accomplish a task. These instructions might have to repeat reasonably to conduct a part of this task. Two such methods of repetition are Recursion and Looping. In recursion, the function calls itself directly or indirectly. In Looping/Iteration, some groups of code will execute repeatedly.
Now, we will be diving deep into these two methods by considering the differences, Time and Space complexities, and Pros and Cons, To decide what we will be choosing in our next algorithmic design.

2. Difference between Recursion and Looping with an Example of Binary Search

Binary search is a search algorithm used to locate an element in a sorted array. The sorted array and a key are the inputs to the algorithm, and the index of the key is our desired output. The algorithm will return the index if it exists in the array and -1 if the element does not exist.

This algorithm will divide the array into begin and last indexes and use these pointers to search the element. The begin index will be positioned 0, and the last will be n-1. During every step, the mid value of the array is found and compared with the key value.

We have three possibilities during the comparison.

  1. The key value matches the mid value.
  2. The key value is less than the mid value (arr[mid] > key). In this case, we get a new search space by removing all the elements on the right, including the mid value.
  3. The key value is higher than the mid value (arr[mid] < key). We will be removing all the elements on the left, including the mid value in this case.

We can implement this in both iterative and recursive ways.

2.1 Iterative Binary Search

Here the input is the sorted array and the key element to be searched. A while loop is defined to carry out the three conditions of the binary search. This loop continues iterating until the target is found (return element) or the beginning index is higher than the last index (return -1).

2.2. Recursive Binary Search

The logic is similar except in the recursive approach, the while loop is replaced with a callback and the values for low and high are passed to the next recursive along with the array and key. When the low > high condition is encountered in the while loop, it exits before the beginning of the next while loop and returns False.

2.3 Time and Space complexities

  • The time complexity is O(log n) which is the same for both recursive and iterative binary searches.
  • The space complexity for the iterative method is O(1) and for the recursive method O(log n). The former is an efficient approach here as a constant space is allocated for the assignment of variables and calling the function.

3. Difference between Recursion and Iterative with an Example of the Fibonacci Series

The Fibonacci series is the addition of two previous numbers to get the subsequent number. The initial elements of the series are F₀= 0 and F₁ = 1. For example, we can calculate the 5 terms of the Fibonacci series as follows. F₅ = 0, 1, 1, 2, 3, 5

3.1. Iterative Fibonacci Series

Here, the value of n is taken. If this is greater than or equal to n, it will return n. Otherwise, we use a for loop to find the sum of the first two elements and continue interchanging them.

3.2. Recursive Fibonacci Series

In the case of the recursive Fibonacci series, the values are computed repeatedly by calling the function again and again.

3.3 Time and Space complexities

  • The time complexity for the iterative method is O(n) and for the recursive method O(2ⁿ). The polynomial time is better than the exponential time. Hence the iterative approach is better than the recursive approach in terms of time complexity.
  • The space complexity for the iterative method is O(1) and for the recursive method O(n). As the values are computed, again and again, the space complexity is higher in recursion.

4. Pros and Cons

  • The approach is easier to understand and the code is quite simpler in Recursion while not in Iteration.
  • The recursive method is expensive in terms of space complexity and might crash the system if it is called infinitely. The iteration method is inexpensive on the processor and consumes CPU cycles when the loop is infinitely called.

5. Conclusion

We can choose recursion and iteration based on the situation. It is better to avoid recursion when memory is of concern. As easier solutions might not be the best ones. But they are easier to debug.

Happy experimenting with Iteration and Recursion !!!

--

--

Varsha C Bendre

Another coffee obsessed Data Scientist who loves to explore mathematics behind the algorithms!!!