***Welcome to ashrafedu.blogspot.com * * * This website is maintained by ASHRAF***

    Tuesday, August 16, 2022

    Iteration versus Recursion

    Recursion is a top–down approach of problem solving. It divides the problem into pieces or selects one key step, postponing the rest.

    On the other hand, iteration is more of a bottom–up approach. It begins with what is known and from this constructs the solution step by step.

    It is hard to say that the non-recursive version is better than the recursive one or vice versa. Few languages do not support writing recursive code. The non-recursive version is more efficient as the overhead of parameter passing in most compilers is heavy.

    Demerits of Recursive Algorithms

    Although recursive algorithms have many merits, they have their limitations. They are:

    1. Many programming languages do not support recursion; hence, recursive mathematical function is to be implemented using iterative methods.

    2. Even though mathematical functions can be easily implemented using recursion, it is always at the cost of additional execution time and memory space.

    3. A recursive function can be called from within or outside itself, and to ensure proper functioning, it has to save the return addresses in some order so that the return to the proper location will yield the desired result when the return to a calling statement is made.

    Demerits of Iterative Methods

    Although the iterative method has various merits, it has its own limitations such as:

    1. Iterative code is not readable and hence not easy to understand.

    2. In iterative techniques, looping of statements is necessary and needs a complex logic.

    3. The iterations may result in a lengthy code.

    No comments:

    Post a Comment

    Prim’s algorithm for finding MST (Minimum Spanning Tree)

    Prim's algorithm to find minimum cost spanning tree uses the greedy approach. Prim's algorithm, in contrast with Kruskal's algor...