For marketers
who love technology
Home » , » Algorithms: a comparison of iterative and recursive approaches

Algorithms: a comparison of iterative and recursive approaches

Programmers often have to answer this question: what's the best, an iterative or a recursive implementation?

The answer is clear: it depends on the problem!

Recursive algorithms are memory intensive

  • Recursive implementations, where a function calls itself to solve a problem, are often elegant and concise. However, they require the computer to store the context of all recursive calls in memory. When memory is not a problem, recursion is a good choice.

Iterative approach can be overly complex

  • Iterative implementations, are sometimes more complex to write, but more memory efficient. A good exercise: try to implement Dijkstra's algorithm in Python in an iterative way, based on my post describing this algorithm. You'll see that it is much more difficult than with a recursive approach (example implementation here).

Recursive dolls ! Those Russian programmers are strong :) (Credit: wikipedia)

SHARE

About Gilles

0 comments :

Post a Comment