An Iterative algorithm will use looping statements such as for loop, while loop or do-while loop to repeat the same steps while a Recursive algorithm, a module function calls itself again and again till the base condition stopping condition is satisfied.
An Iterative algorithm will be faster than the Recursive algorithm because of overheads like calling functions and registering stacks repeatedly.
Many times the recursive algorithms are not efficient as they take more space and time. Recursive algorithms are mostly used to solve complicated problems when their application is easy and effective.
For example Tower of Hannoi algorithm is made easy by recursion while iterations are widely used, efficient and popular. A practical problem: The birthday guy cuts the birthday cake and has to ensure that everyone in the room gets a slice. Solution 1 — Iterative : The birthday guy uses a tray and goes around giving everyone a slice. Solution 2 — Recursive : Take a slice of cake from the tray and pass the tray to the next person who takes a slice from the tray and passes the tray to the next person, who takes a slice from the tray and passes the tray to the next person….
Skip to content. Chapter Content. Recursive vs Iterative Algorithms: Approach: In recursive approach, the function calls itself until the condition is met, whereas, in iterative approach, a function repeats until the condition fails.
Programming Construct Usage: Recursive algorithm uses a branching structure, while iterative algorithm uses a looping construct. Termination Test: Iteration terminates when the loop-continuation condition fails; recursion terminates when a base case is recognized.
Infinite Call: An infinite loop occurs with iteration if the loop-continuation test never becomes false; infinite recursion occurs if the recursion step does not reduce the problem in a manner that converges on the base case.
Like this: Like Loading This can be done by using special variables There are programming languages, such as Prolog, that called accumulators that will be used to keep intermediate do not possess built-in iterative structures and so recursion results and facilitate acceleration.
Nevertheless, there are ways to write To demonstrate the technique, the calculation of the sum recursive programs that have similar behaviour with that of of all integers from 1 to N will be used. The recursive rela- the corresponding iterative programs. To illustrate the problems Luger, such as Prolog Bratko, ; Ra- computational effort of the above implementation, the trace machandran, , LISP Lamkins, ; Seibel, or of the call to find the sum of all integers from 1 to 4 is shown Scheme Dybvig, ; Watson, The Prolog language below Figure 1.
Prolog the call sum 1,S3 stops the recursion and initiates a series of is a Logic Programming Language Bramer, that value returns, until the initial call sum 4,S is reached. The uses heavily recursion. In Prolog, a clause limit case. After that, values of intermediate call are returned can be iterative even if it contains a recursive call. That is, a until the initial call is answered. Transforming Recursion to Iteration in Programming Figure 1. The trace of the recursive implementation Figure 2.
Trace of the iterative implementation, involving two additional parameters, one accumulator and one counter T? This limit case at sum1 4,4,10,S. This means that the second will require the introduction of two new parameters. The stage of returning values and making calculations that is first one will play the role of the counter of the iteration present in the trace of Figure 1 is now avoided, shortening and the second will play the role of the accumulator. That the whole process. Both the counter and the The accumulator will store the partial result up to the cur- accumulator should be initiated with value 1.
For this reason, rent step of iteration. The code is given below: to 1. The complete code is given below. This kind of Once again, the recursion is transformed into an iteration of iteration is also called Tail Recursion. The introduction of N steps. It is is, the second stage of returning values found in Figure 1, apparent that the recursion is transformed into an iteration is omitted, and additionally, needs less memory space than of N steps. The iterative nature of the above implementation the implementation traced at Figure 2, because only one is illustrated in Figure 2, which shows the trace of the call additional parameter is added instead of two.
0コメント