Lecture # 03 - New
Lecture # 03 - New
Lecture No 2
Problem
Problem
Recursive algorithm:
Algorithm that finds the solution of a given problem by
reducing the problem into smaller versions of itself.
Has one or more base cases.
Implemented using recursive methods.
Recursive method:
Method that calls itself.
Recursive
Recursive 1
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 3 2 9 7 1 5 4
8 9 7 5
9 7
2*fact(1) 2*1
fact(1): stack = [1,2,3,4]
fact(1) 1
fact(n)
If n<=1 then
return←1 Sequence of Back to
else recursive the calling function
return ←n*fact(n-1) calls
S.
No Recursion Iteration
.
Disadvantages:
If the base case is not reached or not defined, then
the stack overflow problem may arise.
Time and Space complexity is increased.
Recursion is generally slower than iteration.
CONCLUSION