100% found this document useful (1 vote)
159 views

Lecture# 12-Recursive and Iterative Algorithms

Iterative and recursive algorithms are two techniques used for problem solving. Iterative algorithms use loops to repeat steps, while recursive algorithms use function calls where a function calls itself. Recursive algorithms are generally slower but use less memory, while iterative algorithms are faster but use more memory. Common examples of recursive algorithms include calculating factorials and Fibonacci sequences.

Uploaded by

Shahzad Ashraf
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
159 views

Lecture# 12-Recursive and Iterative Algorithms

Iterative and recursive algorithms are two techniques used for problem solving. Iterative algorithms use loops to repeat steps, while recursive algorithms use function calls where a function calls itself. Recursive algorithms are generally slower but use less memory, while iterative algorithms are faster but use more memory. Common examples of recursive algorithms include calculating factorials and Fibonacci sequences.

Uploaded by

Shahzad Ashraf
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 17

Lecture# 12

Recursive and Iterative Algorithms

By: Dr. Shahzad Ashraf


Overview about Recursive and Iterative Algorithms
• Iteration and recursion are key computer program writing techniques used in creating algorithms and developing the software
• Both are based on a control structure and mainly used for different problem solving approaches, carrying some key
differences as highlighted

Iterative Recursive
o Uses looping statements such o Uses a function calls itself
as for loop, while loop or do- again and again till the base
while loop to repeat the same condition (stopping condition)
steps. is satisfied.
o Uses the repetition structure o Uses the selection structure
o An infinite loop occurs with o Infinite recursion occurs if the
iteration if the loop-condition recursion step does not reduce
test never becomes false. the problem in a manner that
converges on some condition.
(base case)
o Faster operation as uses the o Due to overhead of
permanent storage area maintaining stack the operation
is slower
Overview about Recursive and Iterative Algorithms

Iterative Recursive
o Consume less memory o uses exorbitant memory
o Termination occurs when the o Stops when the base condition
loop condition fails is fulfil
o Infinite looping uses CPU o Infinite recursion can crash the
cycles repeatedly system
o Makes tangling code o Makes code smaller
o Rare choice for complex o Best choice for complex
problem problem solving
o Finding average of a data o Applications including,
series, creating multiplication Factorial , Fibonacci Series
table are the basic applications
Overview about Recursive and Iterative Algorithms

Iterative Recursive
int main() int fact(int n){
{ if(n == 0)
int i, n = 5, fact = 1; return 1;
for(i = 1; i <= n; ++i) else
fact = fact * i; return n * factorial(n-1);
printf(“Factorial for 5 is %d”, fact); }
getch(); int main()
} {
printf(“Factorial for 5 is %d”, fact(5));
getch();
}
More about Recursion
More about Recursion
More about Recursion
More about Recursion
Tower of Hanoi
• It is a Puzzle involving the usage of Stacks.
• It was invented by the French mathematician Edouard Lucas in 1883.
• It is all about shifting of discs from source tower to target tower and there
involves three towers.
• One disc can be shifted at once.
• Only top disc can be shifted.
• Larger disks can not be placed on top of smaller disks.
• 64 gold disks, with decreasing sizes, placed on the first tower.
• Source tower having n discs can be solved using (2n -1) moves.
• For instance, if entire 64 disks are required to move from source tower to
destination tower it will take (264 -1) times which is equal to 585 billion years,
more than 42% time to end the universe.
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Tower of Hanoi
Procedure
i.Move top n-1 disk from A → B
ii.Move disk from A → C
iii.Move n-1 disk from B → C

Implementation
void TOH(n, A, B, C)
{
If(n>0)
{
TOH(n-1, A, C, B)
printf(“%d to %d”, A, C);
TOH(n-1, B, A, C)
}
Tower of Hanoi
(1,3)
Tower of Hanoi
(1,2)
Understanding number of calls function (3,2)
(1,3)
(2,1)
(2,3)
1 (1,3)

2 9

3 13
6
4
10
5
14 15
7 8
11 12

Total number of recursive function calls are determined as (2n+1 -1)


The number of shifts can be acquaint as (2n -1)
Cessation

You might also like