Professional Documents
Culture Documents
1.4 Recursion
1.4 Recursion
A recursive algorithm calls itself which usually passes the return value as a
parameter to the algorithm again. This parameter is the input while the
return value is the output
Example: When you are calculating how much money you have in a box filled with Rs. 100
notes. If there are too many notes, then you might just ask your friend to do the same
work by dividing the entire stack into two. Once you both are done with counting, you will
just add up both the results in order to get the total amount.
Process of calling a function itself within its Loops are used to execute the set of instructions
own code repetitively until the condition is false
Termination condition is defined within the Termination condition is defined in the definition
recursive function of the loop
It is slower than iteration due to the overhead It is faster than recursion.
of repeated function calls
It has to update and maintain the stack.
There is no utilization of stack.
It helps in finding the subsequent term (next term) dependent upon the
preceding term (previous term). If we know the previous term in a given series,
then we can easily determine the next term.
𝑛
T(n) = 2T ( ) +n
2
Recursion Tree
Master Theorem
Formulas:
𝑛
1) 𝑖=0 1 = (n+1)
𝑛 𝑛(𝑛+1)
2) 𝑖=0 𝑖 =
2
𝑛 2 𝑛(𝑛+1)(2𝑛+1)
3) 𝑖=0 𝑖 =
6
1
x<1
1−𝑥
x=1 n+1
𝑥 𝑛+1 −1
x>1
𝑥−1
Ans: nlog2n
Ans: log2n
Ans: n
T(n) = T (n-1) +n
Ans: n2