0% found this document useful (0 votes)
59 views

Data Structures: Iterative and Recursive Methods

The document discusses the differences between iterative and recursive methods. Iterative methods use loops to repeatedly execute a set of instructions, while recursive methods involve a function calling itself. The key differences are that recursion uses function calls and has a base case for termination, while iteration uses loops and terminates based on a condition. Recursion typically has a smaller code size but higher time complexity than iteration due to the function call stack. Both approaches can implement the same tasks but involve different tradeoffs in time and space complexity.

Uploaded by

akash ravi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Data Structures: Iterative and Recursive Methods

The document discusses the differences between iterative and recursive methods. Iterative methods use loops to repeatedly execute a set of instructions, while recursive methods involve a function calling itself. The key differences are that recursion uses function calls and has a base case for termination, while iteration uses loops and terminates based on a condition. Recursion typically has a smaller code size but higher time complexity than iteration due to the function call stack. Both approaches can implement the same tasks but involve different tradeoffs in time and space complexity.

Uploaded by

akash ravi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Data Structures

Home / My courses / BCA204A21T / Unit 2 - SEARCHING & SORTING - Binary search methods / ITERATIVE AND RECURSIVE METHODS

ITERATIVE AND RECURSIVE METHODS


Definition: Iterative & Recursion

Comparison between Iterative & Recursion

Properties

Implementation

Analysis of Recursion : Time & Space Complexity

ITERATIVE AND RECURSIVE METHODS

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.

Differences between iterative and recursion

◄ 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

ITERATIVE AND RECURSIVE METHODS


Definition: Iterative & Recursion

Comparison between Iterative & Recursion

Properties

Implementation

Analysis of Recursion : Time & Space Complexity

Differences: Iterative and Recursion

Property Recursion Iteration

A set of instructions repeatedly


Definition Function calls itself.
executed.

Application For functions. For loops.

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.

Code Size Smaller code size Larger 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

ITERATIVE AND RECURSIVE METHODS


Definition: Iterative & Recursion

Comparison between Iterative & Recursion

Properties

Implementation

Analysis of Recursion : Time & Space Complexity

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

◄ 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

ITERATIVE AND RECURSIVE METHODS


Definition: Iterative & Recursion

Comparison between Iterative & Recursion

Properties

Implementation

Analysis of Recursion : Time & Space Complexity

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

◄ 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

ITERATIVE AND RECURSIVE METHODS


Definition: Iterative & Recursion

Comparison between Iterative & Recursion

Properties

Implementation

Analysis of Recursion : Time & Space Complexity

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

◄ COMPARISON BETWEEN SEQUENTIAL AND


Jump to... Quiz on Searching methods ►
BINARY SEARCH

You might also like