Why Recursion over Iteration?

Why Recursion over Iteration?

Recursion is made for solving problems that can be broken down into smaller, repetitive problems. It is especially good for working on things that have many possible branches and are too complex for an iterative approach. One good example of this would be searching through a file system.

Why is recursion preferable over iteration?

If time complexity is the point of focus, and number of recursive calls would be large, it is better to use iteration. However, if time complexity is not an issue and shortness of code is, recursion would be the way to go. ... Hence, usage of recursion is advantageous in shorter code, but higher time complexity.

Why is recursion faster than iteration?

The recursive function runs much faster than the iterative one. The reason is because in the latter, for each item, a CALL to the function st_push is needed and then another to st_pop . In the former, you only have the recursive CALL for each node. Plus, accessing variables on the callstack is incredibly fast.

James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.