In data structures and algorithms, iteration and recursion are two fundamental problem-solving approaches. Both execute instructions repeatedly until the task is completed. But there are significant differences between recursion and iteration in terms of thought processes, implementation approaches, analysis techniques, code complexity and performance.
Sometimes, a coding problem can be solved using both iteration and recursion, but the choice of approach depends on the nature of the problem and which one is more intuitive to use.
Iteration is implemented using a loop, which consists of initialisation, increment of loop variables, code inside the loop body and termination conditions. In contrast, recursion is implemented using function calls and defined in terms of the number of sub-problems, input size of sub-problems, base cases and extra operations required to combine solutions to the sub-problems.
Iterative code repeatedly executes statements in a loop until the problem is solved. In contrast, recursive code solves a problem using smaller subproblems until it reaches a base case.
Both iteration and recursion can be prone to small but critical mistakes, such as an “off-by-one” error in iteration or passing the wrong value to a function parameter in recursion. It is important to test the conditions and variables involved in these approaches to avoid such errors.
Here are some examples of recursive and iterative solutions to the same problem, along with a comparison of their thought process, performance and code simplicity.
Finding the nth factorial can be implemented using either an iterative or a recursive approach. Although the recursive solution may look simpler, it can be slower because of function-call overhead and requires extra space for the call stack.
Both approaches have a time complexity of O(n), but the recursive solution requires O(n) extra space for the call stack. This space requirement can be an important factor when choosing between them.
Recursive pseudocode
factorial(n)
if n == 0
return 1
return n * factorial(n - 1)Iterative pseudocode
factorial(n)
result = 1
for i from 2 to n
result = result * i
return resultThe recursive solution for calculating Fibonacci numbers is simple and easy to understand, but it is highly inefficient, with time complexity of O(2^n) and space complexity of O(n). In contrast, the iterative solution has a time complexity of O(n) and a space complexity of O(1), which makes it much more efficient.
When choosing between iterative and recursive solutions, we need to consider the trade-offs among simplicity, time complexity, and space complexity.
Recursive pseudocode
fib(n)
if n <= 1
return n
return fib(n - 1) + fib(n - 2)Iterative pseudocode
fibonacci(n)
if n <= 1
return n
previousFibonacci = 0
currentFibonacci = 1
for index from 2 to n
nextFibonacci = previousFibonacci + currentFibonacci
previousFibonacci = currentFibonacci
currentFibonacci = nextFibonacci
return currentFibonacciIn this case, both the iterative and recursive binary search solutions look simple, but the recursive solution can be easier to understand. Despite this, the iterative solution is more efficient in terms of time and space complexity. Both solutions have a time complexity of O(log n), but the recursive solution requires O(log n) extra space for the call stack. In contrast, the iterative solution has no recursive-call overhead and requires only O(1) space, which makes it more efficient.
Recursive pseudocode
binarySearch(X, l, r, key)
if l > r
return -1
mid = l + (r - l) / 2
if X[mid] == key
return mid
else if X[mid] > key
return binarySearch(X, l, mid - 1, key)
else
return binarySearch(X, mid + 1, r, key)Iterative pseudocode
binarySearch(X, l, r, key)
while l <= r
mid = l + (r - l) / 2
if X[mid] == key
return mid
else if X[mid] < key
l = mid + 1
else
r = mid - 1
return -1In this case, both iterative and recursive solutions have a time complexity of O(n²). But the iterative solution is more efficient in terms of space complexity. The recursive solution requires O(n) extra space for the call stack, while the iterative solution has no recursive-call overhead and requires only O(1) space.
Iterative pseudocode
insertionSort(X, n)
for i from 1 to n - 1
currentValue = X[i]
j = i - 1
while j >= 0 and X[j] > currentValue
X[j + 1] = X[j]
j = j - 1
X[j + 1] = currentValueRecursive pseudocode
insertionSort(X, n)
if n <= 1
return
insertionSort(X, n - 1)
currentValue = X[n - 1]
i = n - 2
while i >= 0 and X[i] > currentValue
X[i + 1] = X[i]
i = i - 1
X[i + 1] = currentValueHere, recursive code is simple and easy to understand because it involves only one function parameter and a few lines of code. On the other hand, we can also easily implement preorder traversal iteratively using a stack. In the worst case, both recursive and iterative approaches have O(n) space and time complexity.
Recursive pseudocode
preorder(root)
if root is null
return
process(root.data)
preorder(root.left)
preorder(root.right)Iterative pseudocode
iterativePreorder(root)
if root is null
return
treeStack = empty stack
currNode = root
prevNode = null
while treeStack is not empty or currNode is not null
if currNode is not null
process currNode.data
treeStack.push(currNode)
currNode = currNode.left
else
prevNode = treeStack.pop()
currNode = prevNode.rightEnjoy learning, Enjoy algorithms!