Skip to main content

Command Palette

Search for a command to run...

Recursion Vs Iteration

Published
•3 min read•View as Markdown
Recursion Vs Iteration

Introduction

Recursion and Iteration are the basic ways to repeatedly execute a given set of instructions in any programming language. A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).

Recursion

  • Recursion has a top-down approach in problem-solving in which the original problem is divided into smaller sub-problems

  • It is the best way to solve complex problems especially when the problem can be defined in recursive terms

  • Recursive solutions often tend to be shorter and simpler than non-recursive ones

  • Code is cleaner and easier to use

  • Recursion uses the original formula to solve problems

  • It follows a divide and conquer technique to solve problems

  • In some conditions, recursion may be more efficient

  • Recursion is implemented using the system stack. If the stack space on the system is limited, recursion to a deeper level will be difficult to implement

  • It is difficult to find bugs, particularly when using global variables

  • time complexity is very high(generally exponential).

  • Termination of code occurs through the base case, where there will be no function call.

  • When a recursive function is called, some amount of overhead in the form of a run-time stack is always involved. Before jumping to the function with the smaller parameter the return address of the calling function is all stored on the system stack. Therefore, while using recursion a lot of time is needed to first push all the information on the stack

Iteration

  • Iteration follows a bottom-up approach that begins with what is known and then constructing the solutions step-by-step

  • Iteration is when a loop repeatedly executes the set of instructions like "for" loops and "while" loops.

  • Iteration is when we execute a set of instructions repeatedly until the condition controlling the loop becomes false.

  • In iteration, it is necessary to have the right controlling condition; else, the program may go in an infinite loop.

  • Iteration is faster and more space-efficient than recursion.

  • Iteration can be used to repeatedly execute a set of statements without the overhead of function calls and without using stack memory.

  • It's easier to optimize iterative codes, and they generally have polynomial time complexity.

  • In loops, we can go only in one direction, i.e., we can’t go or transfer data from the current state to the previous state that has already been executed

  • It’s difficult to traverse trees/graphs using loops.

  • Relatively lower time complexity(generally polynomial-logarithmic).

  • When the termination condition for the iterator ceases to be satisfied.

Conclusion

Since Python does not store anything about previous iteration steps, iteration is quite faster and more memory-efficient than recursion. In terms of time complexity and memory constraints, iteration is preferred over recursion.

More from this blog

GCP Associate Engineer Exam training (Storage and database)

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––Section 1: Cloud Storage (25 Questions)–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– Question:...

Jun 19, 202554 min read

GCP Associate Engineer Exam training (Compute Engine, gke, app engine, cloud functions, cloud run)

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––Section 1: Compute Engine (25 Questions)–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– Question: Your ML training needs maximum CPU performance...

Jun 19, 202526 min read

GCP Associate Engineer Exam training (Cloud Computing, Cloud Deployment Models and Cloud Service Models)

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––Section 1: Cloud Computing Fundamentals–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––– Question:...

Jun 18, 202521 min read

Anusree Anilkumar's Blog

16 posts