Professional Documents
Culture Documents
Lecture 3
Lecture 3
Lecture 3
Algorithm
Lecture 3: Recursion
Recursion
2
Properties of asymptotic notations
Transitivity
Reflexivity
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 for(k=1;k≤133;)
(𝑘=1;𝑘≤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.