Data Structures: Iterative and Recursive Methods
Data Structures: Iterative and Recursive Methods
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Binary search methods / ITERATIVE AND RECURSIVE METHODS
Properties
Implementation
The recursion and iteration both repeatedly execute the set of instructions. Recursion is when a statement in a function calls itself repeatedly. The iteration is when a
loop repeatedly executes until the controlling condition becomes false. The primary difference between recursion and iteration is that recursion is a process, always
applied to a function and iteration is applied to the set of instructions which we want to get repeatedly executed.
Properties
Implementation
Through base case, where there When the termination condition for
Termination
will be no function call. the iterator ceases to be satisfied.
Used when code size needs to Used when time complexity needs
Usage be small, and time complexity is to be balanced against an
not an issue. expanded code size.
Time Complexity Very high time complexity. Relatively lower time complexity
Properties
◄ COMPARISON BETWEEN SEQUENTIAL AND
Jump to... Quiz on Searching methods ►
BINARY SEARCH
Data Structures
Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Binary search methods / ITERATIVE AND RECURSIVE METHODS
Properties
Implementation
Properties
A recursive function can go infinite like a loop. To avoid infinite running of recursive function, there are two properties that a recursive function must have:
Base criteria − There must be at least one base criteria or condition, such that, when this condition is met the function stops calling itself recursively.
Progressive approach − The recursive calls should progress in such a way that each time a recursive call is made it comes closer to the base criteria.
Implementation
Properties
Implementation
Implementation
Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller
function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.
This implies, the caller function has to suspend its execution temporarily and resume later when the execution control returns from the callee function. Here, the caller
function needs to start exactly from the point of execution where it puts itself on hold. It also needs the exact same data values it was working on. For this purpose, an
activation record (or stack frame) is created for the caller function.
This activation record keeps the information about local variables, formal parameters, return address and all information passed to the caller function.
Analysis of Recursion
Properties
Implementation
Analysis of Recursion
One may argue why to use recursion, as the same task can be done with iteration. The first reason is, recursion makes a program more readable and because of latest
enhanced CPU systems, recursion is more efficient than iterations.
Time Complexity
In case of iterations, we take number of iterations to count the time complexity. Likewise, in case of recursion, assuming everything is constant, we try to figure out the
number of times a recursive call is being made. A call made to a function is Ο(1), hence the number of times a recursive call is made makes the recursive function Ο .
Space Complexity
Space complexity is counted as what amount of extra space is required for a module to execute. In case of iterations, the compiler hardly requires any extra space. The
compiler keeps updating the values of variables used in the iterations. But in case of recursion, the system needs to store activation record each time a recursive call is
made. Hence, it is considered that space complexity of recursive function may go higher than that of a function with iteration.
Challenge Questions