Lecture 3
Lecture 3
Analysis of
Algorithm
Lecture 3: Recursion
Dr. Tauheed
Contents
Asymptotic
notation
Recursion
2
Properties of asymptotic notations
1. 2. 3.
Transpose
if and only if
Symmetry:
Questions
(1) (2) (3) Let k and m are constants
main() 8⇒1 16 ⇒ 1
{ 4⇒2 8⇒ 2
int n; 4⇒3
while(n>1) 2⇒3 2⇒ 4
{ 1⇒?? 1⇒??
n=n/2
}
} Complexity=
𝑚𝑎𝑖𝑛()
Problem 1 Problem 2 Problem 3
main()
𝑓𝑜𝑟 (𝑖=1;𝑖≤𝑛;)
{ {
for (i=1;i≤n;)
𝑓𝑜𝑟 (𝑗=1;𝑗≤𝑛;)
{ {
for(j=1;j≤i^2;)
𝑓𝑜r
{ {
(𝑘=1;𝑘≤133;)
for(k=1;k≤133;)
{
𝑥=𝑦+𝑎
{ x=y+a
}
} }
} }
} }
}
Analyzing Insertion Sort
Worst Case 𝑂 ( 𝑛2 )
Average Case 𝑂 ( 𝑛2 )
Recursion
Definition
A function calling itself to solve a particular problem is called a
recursion
e.g.
Properties of recursion
•Recursive step
•Termination condition
•There should be change in its parametric value
e.g.