Raghunath Tewari
rtewari@cse.iitk.ac.in
What is Recursion?
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
- Fibonacci numbers: F (n) = F (n 1) + F (n 2)
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
- Fibonacci numbers: F (n) = F (n 1) + F (n 2)
The argument must change in between calls.
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
- Fibonacci numbers: F (n) = F (n 1) + F (n 2)
The argument must change in between calls.
There must be one or more base case (also known as stopping
condition).
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
- Fibonacci numbers: F (n) = F (n 1) + F (n 2)
The argument must change in between calls.
There must be one or more base case (also known as stopping
condition).
- Factorial: 1! = 1.
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
- Fibonacci numbers: F (n) = F (n 1) + F (n 2)
The argument must change in between calls.
There must be one or more base case (also known as stopping
condition).
- Factorial: 1! = 1.
- Fibonacci numbers: F (1) = 1 and F (2) = 1.
What is Recursion?
Recursive Function in C
A function defined in terms of itself is called a recursive function.
The process is known as recursion.
Some examples:
- Factorial: n! = n (n 1)!
- Fibonacci numbers: F (n) = F (n 1) + F (n 2)
The argument must change in between calls.
There must be one or more base case (also known as stopping
condition).
- Factorial: 1! = 1.
- Fibonacci numbers: F (1) = 1 and F (2) = 1.
Argument values move towards the base case.
Recursion in C
Example of a Recursive Function Factorial Function
1 #include<stdio.h>
2
3 int fact(int n){
4 if (n==0) return 1;
5 else return n*fact(n-1);
6 }
7
8 int main(){
9 int n, f;
10 scanf("%d", &n);
11 f = fact(n);
12 printf("Factorial(%d) = %d\n
",n,f);
13 return 0;
14 }
Recursion in C
Example of a Recursive Function Factorial Function
Recursion in C
Example of a Recursive Function Factorial Function
Recursion in C
Example of a Recursive Function Factorial Function
Recursion in C
Example of a Recursive Function Factorial Function
Recursion in C
Example of a Recursive Function Factorial Function
Recursion in C
Memory Usage by Recursive Functions
Recursion in C
Memory Usage by Recursive Functions
Recursion in C
Memory Usage by Recursive Functions
fact(0)
Recursion in C
Memory Usage by Recursive Functions
fact(0)
Recursion in C
Memory Usage by Recursive Functions
fact(0)
Recursion in C
Memory Usage by Recursive Functions
fact(0)
Recursion in C
Memory Usage by Recursive Functions
fact(0)
Recursion in C
Memory Usage by Recursive Functions
0
fact(0)
n
Whenever a call is made to a 2
fact(1)
function, space is reserved in n
the stack for the function 2
arguments and the variables fact(2)
n
declared inside the function. 3
fact(3)
For recursive functions, n
separate space is created for 3
main
each call to the function. n f
Recursion in C
Memory Usage by Recursive Functions
0
fact(0)
n
Whenever a call is made to a 2
fact(1)
function, space is reserved in n
the stack for the function 2
arguments and the variables fact(2)
n
declared inside the function. 3
fact(3)
For recursive functions, n
separate space is created for 3
main
each call to the function. n f
Order of
Function Calls
Order of Execution: fact(3) = 3*fact(2)= 3*(2*fact(1))=
3*(2*(1*fact(0)))= 3*(2*(1*1))= 3*(2*1)= 3*2 = 6
Recursion in C
Recursion in C
Memory Usage by Recursive Functions
Return Values
0
fact(0) 1
n
Whenever a call is made to a 2
fact(1) 1
function, space is reserved in n
the stack for the function 2
arguments and the variables fact(2) 2
n
declared inside the function. 3
fact(3) 6
For recursive functions, n
separate space is created for 3
main
each call to the function. n f
Order of
Function Calls
Order of Execution: fact(3) = 3*fact(2)= 3*(2*fact(1))=
3*(2*(1*fact(0)))= 3*(2*(1*1))= 3*(2*1)= 3*2 = 6
Recursion in C
Recursion in C
Memory Usage by Recursive Functions
Return Values
0
fact(0) 1
n
Whenever a call is made to a 2
fact(1) 1
function, space is reserved in n
the stack for the function 2
arguments and the variables fact(2) 2
n
declared inside the function. 3
fact(3) 6
For recursive functions, n
separate space is created for 3 6
main
each call to the function. n f
Order of
Function Calls
Recursion in C
Memory Usage by Recursive Functions
Return Values
0
fact(0) 1
n
Whenever a call is made to a 2
fact(1) 1
function, space is reserved in n
the stack for the function 2
arguments and the variables fact(2) 2
n
declared inside the function. 3
fact(3) 6
For recursive functions, n
separate space is created for 3 6
main
each call to the function. n f
Order of
Function Calls
Order of Execution: fact(3) = 3*fact(2)= 3*(2*fact(1))=
3*(2*(1*fact(0)))= 3*(2*(1*1))= 3*(2*1)= 3*2 = 6
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Recursion in C
Example n choose k (Definition)
Recursion in C
Example n choose k (Definition)
Recursive Definition
n n1 n1
= + for 1 k n 1
k k 1 k
n n
= = 1 for n 0 (Base Case)
0 n
Recursion in C
Example n choose k (Definition)
Recursive Definition
n n1 n1
= + for 1 k n 1
k k 1 k
n n
= = 1 for n 0 (Base Case)
0 n
Note: In each recursive step, value of n decreases. Value of k may
or may not decrease. Recursion stops when n = k or k = 0.
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Recursion in C
Example n choose k (Program)
1 #include<stdio.h>
2
3 int choose(int n, int k){
4 if ((n == k) || (k == 0))
5 return 1;
6 else
7 return (choose(n-1,k-1) +
choose(n-1,k));
8 }
9
10 int main(){
11 int n,k;
12 scanf("%d",&n);
13 scanf("%d",&k);
14 printf("%d\n",choose(n,k));
15 return 0;
16 }
Recursion in C
Example n choose k (Program)
1 #include<stdio.h>
2 Notethat to obtain the value
3 int choose(int n, int k){ of kn , two recursive calls
4 if ((n == k) || (k == 0))
5 return 1;
needto be made one to
n1 n1
6 else k1 and one to k .
7 return (choose(n-1,k-1) +
choose(n-1,k));
8 }
9
10 int main(){
11 int n,k;
12 scanf("%d",&n);
13 scanf("%d",&k);
14 printf("%d\n",choose(n,k));
15 return 0;
16 }
Recursion in C
Example n choose k (Program)
1 #include<stdio.h>
2 Notethat to obtain the value
3 int choose(int n, int k){ of kn , two recursive calls
4 if ((n == k) || (k == 0))
5 return 1;
needto be made one to
n1 n1
6 else k1 and one to k .
7 return (choose(n-1,k-1) + Known as double recursion.
choose(n-1,k));
8 }
9
10 int main(){
11 int n,k;
12 scanf("%d",&n);
13 scanf("%d",&k);
14 printf("%d\n",choose(n,k));
15 return 0;
16 }
Recursion in C
Example n choose k (Program)
1 #include<stdio.h>
2 Notethat to obtain the value
3 int choose(int n, int k){ of kn , two recursive calls
4 if ((n == k) || (k == 0))
5 return 1;
needto be made one to
n1 n1
6 else k1 and one to k .
7 return (choose(n-1,k-1) + Known as double recursion.
choose(n-1,k));
8 } How many calls to the
9 function choose is made when
10 int main(){ n=6 and k=3?
11 int n,k;
12 scanf("%d",&n);
13 scanf("%d",&k);
14 printf("%d\n",choose(n,k));
15 return 0;
16 }
Recursion in C
Example n choose k (Program)
1 #include<stdio.h>
2 Notethat to obtain the value
3 int choose(int n, int k){ of kn , two recursive calls
4 if ((n == k) || (k == 0))
5 return 1;
needto be made one to
n1 n1
6 else k1 and one to k .
7 return (choose(n-1,k-1) + Known as double recursion.
choose(n-1,k));
8 } How many calls to the
9 function choose is made when
10 int main(){ n=6 and k=3?
11 int n,k;
12 scanf("%d",&n); 37 calls!
13 scanf("%d",&k); No. of calls grow very fast
14 printf("%d\n",choose(n,k));
15 return 0;
(exponentially) with
16 } increasing values of n and k.
Recursion in C
Example n choose k (Function Call Tree)
Recursion in C
Example n choose k (Function Call Tree)
(4,1) (4,2)
(3,0) (3,1)
(3,1) (3,2)
1 (2,0) (2,1)
(2,0) (2,1) (2,1) (2,2)
1 (1,0) (1,1)
1 (1,0) (1,1) (1,0) (1,1) 1
1 1
1 1 1 1
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
= choose(2,0) + choose(2,1) + choose(3,2)
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
= choose(2,0)+ choose(2,1) + choose(3,2)
= 1 + choose(2,1) + choose(3,2)
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
= choose(2,0) + choose(2,1) + choose(3,2)
= 1+ choose(2,1) + choose(3,2)
= 1+ choose(1,0) + choose(1,1) + choose(3,2)
= 1+ 1 + choose(1,1) + choose(3,2)
= 1+ 1 + 1 + choose(3,2)
= 1+ 1 + 1 + choose(2,1) + choose(2,2)
= 1+ 1 + 1 + choose(1,0) + choose(1,1)
+ choose(2,2)
= 1 + 1 + 1 + 1 + choose(1,1) + choose(2,2)
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
= choose(2,0) + choose(2,1) + choose(3,2)
= 1+ choose(2,1) + choose(3,2)
= 1+ choose(1,0) + choose(1,1) + choose(3,2)
= 1+ 1 + choose(1,1) + choose(3,2)
= 1+ 1 + 1 + choose(3,2)
= 1+ 1 + 1 + choose(2,1) + choose(2,2)
= 1+ 1 + 1 + choose(1,0) + choose(1,1)
+ choose(2,2)
= 1 + 1 + 1 + 1 + choose(1,1) + choose(2,2)
= 1 + 1 + 1 + 1 + 1 + choose(2,2)
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
= choose(2,0) + choose(2,1) + choose(3,2)
= 1+ choose(2,1) + choose(3,2)
= 1+ choose(1,0) + choose(1,1) + choose(3,2)
= 1+ 1 + choose(1,1) + choose(3,2)
= 1+ 1 + 1 + choose(3,2)
= 1+ 1 + 1 + choose(2,1) + choose(2,2)
= 1+ 1 + 1 + choose(1,0) + choose(1,1)
+ choose(2,2)
= 1 + 1 + 1 + 1 + choose(1,1) + choose(2,2)
= 1 + 1 + 1 + 1 + 1 + choose(2,2)
= 1+1+1+1+1+1
Recursion in C
Example n choose k (Order of Execution)
For n = 4 and k = 2.
choose(4,2) = choose(3,1) + choose(3,2)
= choose(2,0) + choose(2,1) + choose(3,2)
= 1+ choose(2,1) + choose(3,2)
= 1+ choose(1,0) + choose(1,1) + choose(3,2)
= 1+ 1 + choose(1,1) + choose(3,2)
= 1+ 1 + 1 + choose(3,2)
= 1+ 1 + 1 + choose(2,1) + choose(2,2)
= 1+ 1 + 1 + choose(1,0) + choose(1,1)
+ choose(2,2)
= 1 + 1 + 1 + 1 + choose(1,1) + choose(2,2)
= 1 + 1 + 1 + 1 + 1 + choose(2,2)
= 1+1+1+1+1+1
= 6
Recursion in C
Advantages and Disadvantages
Advantages
Recursion in C
Advantages and Disadvantages
Advantages
- Elegant. Solution is cleaner.
Recursion in C
Advantages and Disadvantages
Advantages
- Elegant. Solution is cleaner.
- Fewer variables.
Recursion in C
Advantages and Disadvantages
Advantages
- Elegant. Solution is cleaner.
- Fewer variables.
- Once the recursive definition is figured out, program is easy to
implement.
Disadvantages
Recursion in C
Advantages and Disadvantages
Advantages
- Elegant. Solution is cleaner.
- Fewer variables.
- Once the recursive definition is figured out, program is easy to
implement.
Disadvantages
- Debugging can be considerably more difficult.
Recursion in C
Advantages and Disadvantages
Advantages
- Elegant. Solution is cleaner.
- Fewer variables.
- Once the recursive definition is figured out, program is easy to
implement.
Disadvantages
- Debugging can be considerably more difficult.
- Figuring out the logic of the recursive function is not easy
sometimes.
Recursion in C
Advantages and Disadvantages
Advantages
- Elegant. Solution is cleaner.
- Fewer variables.
- Once the recursive definition is figured out, program is easy to
implement.
Disadvantages
- Debugging can be considerably more difficult.
- Figuring out the logic of the recursive function is not easy
sometimes.
- In general, can be inefficient (requires more time and space).
String Reverse
Recursive Definition:
- reverse(aw)= reverse(w)a
- where w is a string and a is a single character.
Base Case:
- reverse(w)= w
- when w is the empty string (string of length 0).
1 #include <stdio.h>
2 void reverse(){
3 char c;
4 scanf("%c",&c);
5 if( c != \n){
6 reverse();
7 printf("%c",c);
8 }
9 }
10
11 int main(){
12 reverse();
13 printf("\n");
14 return 0;
15 }
Recap
Recap
What is recursion?
Recap
What is recursion?
Two components:
Recap
What is recursion?
Two components:
- recursive definition, and
Recap
What is recursion?
Two components:
- recursive definition, and
- base case.
Recap
What is recursion?
Two components:
- recursive definition, and
- base case.
Function stack.
Recap
What is recursion?
Two components:
- recursive definition, and
- base case.
Function stack.
Advantages and disadvantages.
Recursive Definition:
Recursive Definition:
- r max1({a0 , a1 , . . . , an1 }, n) = max(a0 , r max1({a1 , . . . , an1 }, n 1))
Recursive Definition:
- r max1({a0 , a1 , . . . , an1 }, n) = max(a0 , r max1({a1 , . . . , an1 }, n 1))
- r max1({a0 }, 1) = a0
Recursive Definition:
- r max1({a0 , a1 , . . . , an1 }, n) = max(a0 , r max1({a1 , . . . , an1 }, n 1))
- r max1({a0 }, 1) = a0
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar;
3 else return max(*ar,r_max1(
ar+1,n-1));
4 }
5
6 int max(int a, int b){
7 if (a>b) return a;
8 else return b;
9 }
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar;
3 else return max(*ar,r_max1(
ar+1,n-1));
4 }
5
6 int max(int a, int b){
7 if (a>b) return a;
8 else return b;
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar;
3 else return max(*ar,r_max1(
ar+1,n-1));
4 }
5
6 int max(int a, int b){
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3)
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar;
3 else return max(*ar,r_max1(
ar+1,n-1));
4 }
5
56 2
r max1(56,2)
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3)
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar;
3 else return max(*ar,r_max1( 60 1
r max1(60,1)
ar+1,n-1)); ar n
4 }
5
56 2
r max1(56,2)
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3)
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar; Return Values
3 else return max(*ar,r_max1( 60 1
r max1(60,1)
ar+1,n-1)); ar n
4 }
5
56 2
r max1(56,2)
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3)
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar; Return Values
3 else return max(*ar,r_max1( 60 1
ar+1,n-1));
r max1(60,1) 7
ar n
4 }
5
56 2
r max1(56,2)
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3)
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar; Return Values
3 else return max(*ar,r_max1( 60 1
ar+1,n-1));
r max1(60,1) 7
ar n
4 }
5
56 2
r max1(56,2) 11
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3)
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar; Return Values
3 else return max(*ar,r_max1( 60 1
ar+1,n-1));
r max1(60,1) 7
ar n
4 }
5
56 2
r max1(56,2) 11
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3) 11
ar n
9 }
48 4
r max1(48,4)
ar n
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar; Return Values
3 else return max(*ar,r_max1( 60 1
ar+1,n-1));
r max1(60,1) 7
ar n
4 }
5
56 2
r max1(56,2) 11
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3) 11
ar n
9 }
48 4
r max1(48,4) 11
ar n
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int r_max1(int *ar, int n){
2 if (n==1) return *ar; Return Values
3 else return max(*ar,r_max1( 60 1
ar+1,n-1));
r max1(60,1) 7
ar n
4 }
5
56 2
r max1(56,2) 11
6 int max(int a, int b){ ar n
7 if (a>b) return a; 52 3
8 else return b; r max1(52,3) 11
ar n
9 }
48 4
r max1(48,4) 11
ar n
Note that the function r_max1 makes one call to itself in the
function definition.
Time is about n and space is equivalent to stack depth (which
is n).
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int max(int a, int b){
2 if (a>b) return a;
3 else return b;
4 }
5 int r_max2(int *ar, int n){
6 if (n==1) return *ar;
7 else return max(r_max2(ar,n
/2),r_max2(ar+(n/2),(n
+1)/2));
8 }
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int max(int a, int b){
2 if (a>b) return a; Function Call Tree to r_max2 for
3 else return b; the above array.
4 }
5 int r_max2(int *ar, int n){
6 if (n==1) return *ar;
7 else return max(r_max2(ar,n
/2),r_max2(ar+(n/2),(n
+1)/2));
8 }
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int max(int a, int b){
2 if (a>b) return a; Function Call Tree to r_max2 for
3 else return b; the above array.
4 } (48,4)
5 int r_max2(int *ar, int n){
6 if (n==1) return *ar;
7 else return max(r_max2(ar,n (48,2) (56,2)
/2),r_max2(ar+(n/2),(n
+1)/2));
(48,1) (52,1) (56,1) (60,1)
8 }
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int max(int a, int b){
2 if (a>b) return a; Function Call Tree to r_max2 for
3 else return b; the above array.
4 } (48,4)
5 int r_max2(int *ar, int n){
6 if (n==1) return *ar;
7 else return max(r_max2(ar,n (48,2) (56,2)
/2),r_max2(ar+(n/2),(n
+1)/2));
(48,1) (52,1) (56,1) (60,1)
8 }
- Number of function calls:
The function r_max2 2n-1
makes two calls to itself (constant time per call).
in the function definition.
ar 48 7 5 11 7
Addresses 48 52 56 60
1 int max(int a, int b){
2 if (a>b) return a; Function Call Tree to r_max2 for
3 else return b; the above array.
4 } (48,4)
5 int r_max2(int *ar, int n){
6 if (n==1) return *ar;
7 else return max(r_max2(ar,n (48,2) (56,2)
/2),r_max2(ar+(n/2),(n
+1)/2));
(48,1) (52,1) (56,1) (60,1)
8 }
- Number of function calls:
The function r_max2 2n-1
makes two calls to itself (constant time per call).
in the function definition. - Stack Depth: log (n)
(space used).
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Example
Binary Search
Program
Binary Search
Program
Binary Search
Analysis
Binary Search
Analysis
Binary Search
Analysis
Binary Search
Analysis
Binary Search
Analysis
Announcements
Announcements
Announcements
Announcements
Global Variables
Global Variables
Global Variables
Global Variables
1 #include<stdio.h>
2 int g=10, h=20;
3
4 int add(){
5 return g+h;
6 }
7
8 int main(){
9 printf("%d %d %d\n",g,h,add());
10 return 0;
11 }
10 20 30
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Global Variables
1 #include<stdio.h>
2 int g=10, h=20;
3
4 int add(){
5 return g+h;
6 }
7
8 int main(){
9 printf("%d %d %d\n",g,h,add());
10 return 0;
11 }
Output: 10 20 30
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Global Variables
1 #include<stdio.h>
2 int g=10, h=20;
3
4 int add(){
5 return g+h;
6 }
7
8 int main(){
9 printf("%d %d %d\n",g,h,add());
10 return 0;
11 }
Output: 10 20 30
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Global Variables
Output: 10 20 30
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Global Variables
Global Variables
1 #include<stdio.h>
2 int count = 0;
3
4 int fib(int n){ Output:
5 count = count+1;
6 if (n==1 || n==2) return 1;
7 else return fib(n-1) + fib(n-2);
8 }
9
10 int main(){
11 int num;
12 scanf("%d",&num);
13 printf("%d, %d\n",fib(num),count);
14 return 0;
15 }
1 #include<stdio.h>
2 int count = 0;
3
4 int fib(int n){ Output:
5 count = count+1;
6 if (n==1 || n==2) return 1;
Value of num Output
7 else return fib(n-1) + fib(n-2);
8 }
9
10 int main(){
11 int num;
12 scanf("%d",&num);
13 printf("%d, %d\n",fib(num),count);
14 return 0;
15 }
1 #include<stdio.h>
2 int count = 0;
3
4 int fib(int n){ Output:
5 count = count+1;
6 if (n==1 || n==2) return 1;
Value of num Output
7 else return fib(n-1) + fib(n-2); 3 2, 3
8 }
9
10 int main(){
11 int num;
12 scanf("%d",&num);
13 printf("%d, %d\n",fib(num),count);
14 return 0;
15 }
1 #include<stdio.h>
2 int count = 0;
3
4 int fib(int n){ Output:
5 count = count+1;
6 if (n==1 || n==2) return 1;
Value of num Output
7 else return fib(n-1) + fib(n-2); 3 2, 3
8 }
9 10 55, 109
10 int main(){ 20 6765, 13529
11 int num;
12 scanf("%d",&num); 30 832040, 1664079
13 printf("%d, %d\n",fib(num),count);
14 return 0;
15 }
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Then 5 is inserted: {1, 2, 3, 5, 7, 9}
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Then 5 is inserted: {1, 2, 3, 5, 7, 9}
Finally 11 is inserted: {1, 2, 3, 5, 7, 9, 11}
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Then 5 is inserted: {1, 2, 3, 5, 7, 9}
Finally 11 is inserted: {1, 2, 3, 5, 7, 9, 11}
If A has size n and B has size m, how many steps will this
algorithm take in the worst case
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Then 5 is inserted: {1, 2, 3, 5, 7, 9}
Finally 11 is inserted: {1, 2, 3, 5, 7, 9, 11}
If A has size n and B has size m, how many steps will this
algorithm take in the worst case
- Number of steps n + n + . . . (m times) = n m. (Verify
yourself!)
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Then 5 is inserted: {1, 2, 3, 5, 7, 9}
Finally 11 is inserted: {1, 2, 3, 5, 7, 9, 11}
If A has size n and B has size m, how many steps will this
algorithm take in the worst case
- Number of steps n + n + . . . (m times) = n m. (Verify
yourself!)
Now suppose A and B are halves of an array of size n (both
have size n/2).
Sorting an Array
Merging Two Sorted Arrays First Approach
Solution Idea:
- Let A and B be the two sorted arrays.
- For every element in array B, check where it would fit in array
A by scanning from left to right.
Example: Consider 2 sorted arrays A = {1, 2, 7, 9} and
B = {3, 5, 11}.
First 3 is inserted: {1, 2, 3, 7, 9}
Then 5 is inserted: {1, 2, 3, 5, 7, 9}
Finally 11 is inserted: {1, 2, 3, 5, 7, 9, 11}
If A has size n and B has size m, how many steps will this
algorithm take in the worst case
- Number of steps n + n + . . . (m times) = n m. (Verify
yourself!)
Now suppose A and B are halves of an array of size n (both
have size n/2).
Number of steps = n2 /4.
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
A 1 2 7 9 B 3 8 11 14 C 1 2 3
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
A 1 2 7 9 B 3 8 11 14 C 1 2 3
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
A 1 2 7 9 B 3 8 11 14 C 1 2 3
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
A 1 2 7 9 B 3 8 11 14 C 1 2 3
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8 9
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
A 1 2 7 9 B 3 8 11 14 C 1 2 3
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8 9
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8 9 11
Sorting an Array
Merging Two Sorted Arrays Second Approach (Example)
A 1 2 7 9 B 3 8 11 14 C 1
A 1 2 7 9 B 3 8 11 14 C 1 2
A 1 2 7 9 B 3 8 11 14 C 1 2 3
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8 9
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8 9 11
A 1 2 7 9 B 3 8 11 14 C 1 2 3 7 8 9 11 14
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merging Two Sorted Arrays Second Approach (Algorithm)
Sorting an Array
Merge Sort Complete Algorithm
MergeSort(A[0, . . . , n 1], n)
Sorting an Array
Merge Sort Complete Algorithm
MergeSort(A[0, . . . , n 1], n)
1 If n > 1,
1 MergeSort(A[0, . . . , n/2 1], n/2)
2 MergeSort(A[n/2, . . . , n 1], n n/2)
Sorting an Array
Merge Sort Complete Algorithm
MergeSort(A[0, . . . , n 1], n)
1 If n > 1,
1 MergeSort(A[0, . . . , n/2 1], n/2)
2 MergeSort(A[n/2, . . . , n 1], n n/2)
3 Merge(A, n)
Sorting an Array
Merge Sort Complete Algorithm
MergeSort(A[0, . . . , n 1], n)
1 If n > 1,
1 MergeSort(A[0, . . . , n/2 1], n/2)
2 MergeSort(A[n/2, . . . , n 1], n n/2)
3 Merge(A, n)
Description of the functions
MergeSort: Takes an array A of size n and sorts it
Sorting an Array
Merge Sort Complete Algorithm
MergeSort(A[0, . . . , n 1], n)
1 If n > 1,
1 MergeSort(A[0, . . . , n/2 1], n/2)
2 MergeSort(A[n/2, . . . , n 1], n n/2)
3 Merge(A, n)
Description of the functions
MergeSort: Takes an array A of size n and sorts it
Merge: Takes an array A of size n with the assumption that
the first n/2 elements and the remaining n n/2 elements are
sorted. Merges the two arrays and puts it back in A.
Sorting an Array
Merge Sort C Code
1 void merge(int *ar, int n){
2 int *temp, i, j=0, k=n/2;
3 temp = (int *)malloc(sizeof(int)*n);
4 for(i=0; i<n; i++){
5 if ((j < n/2) && (k < n)){
6 if (ar[j] < ar[k]){
7 temp[i] = ar[j];
8 j++; }
9 else{
10 temp[i] = ar[k];
11 k++; }
12 }
13 else if (j == n/2){
14 temp[i] = ar[k];
15 k++; }
16 else{
17 temp[i] = ar[j];
18 j++; }
19 }
20 for (i=0; i<n; i++)
21 ar[i] = temp[i];
22 free(temp);}
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Sorting an Array
Merge Sort C Code
1 void merge(int *ar, int n){
2 int *temp, i, j=0, k=n/2;
3 temp = (int *)malloc(sizeof(int)*n);
4 for(i=0; i<n; i++){ 1 void merge_sort(int *ar, int n){
5 if ((j < n/2) && (k < n)){ 2 if (n>1){
6 if (ar[j] < ar[k]){ 3 merge_sort(ar,n/2);
7 temp[i] = ar[j]; 4 merge_sort(ar+(n/2), n-(n/2));
8 j++; } 5 merge(ar,n);
9 else{ 6 }
10 temp[i] = ar[k]; 7 }
11 k++; } 8
12 } 9 int main(){
13 else if (j == n/2){ 10 int ar1[]={2,5,4,8,6,9,8,6,1,4,7};
14 temp[i] = ar[k]; 11 merge_sort(ar1,11);
15 k++; } 12 display(ar1,11); //displaying array
16 else{ 13 return 0;
17 temp[i] = ar[j]; 14 }
18 j++; }
19 }
20 for (i=0; i<n; i++) 1 2 4 4 5 6 6 7 8 8 9
21 ar[i] = temp[i];
22 free(temp);}
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Sorting an Array
Merge Sort C Code
1 void merge(int *ar, int n){
2 int *temp, i, j=0, k=n/2;
3 temp = (int *)malloc(sizeof(int)*n);
4 for(i=0; i<n; i++){ 1 void merge_sort(int *ar, int n){
5 if ((j < n/2) && (k < n)){ 2 if (n>1){
6 if (ar[j] < ar[k]){ 3 merge_sort(ar,n/2);
7 temp[i] = ar[j]; 4 merge_sort(ar+(n/2), n-(n/2));
8 j++; } 5 merge(ar,n);
9 else{ 6 }
10 temp[i] = ar[k]; 7 }
11 k++; } 8
12 } 9 int main(){
13 else if (j == n/2){ 10 int ar1[]={2,5,4,8,6,9,8,6,1,4,7};
14 temp[i] = ar[k]; 11 merge_sort(ar1,11);
15 k++; } 12 display(ar1,11); //displaying array
16 else{ 13 return 0;
17 temp[i] = ar[j]; 14 }
18 j++; }
19 }
20 for (i=0; i<n; i++) Output: 1 2 4 4 5 6 6 7 8 8 9
21 ar[i] = temp[i];
22 free(temp);}
Raghunath Tewari rtewari@cse.iitk.ac.in ESc101A
Recursion in C
Sorting an Array
Merge Sort Time Analysis (the best part)
Sorting an Array
Merge Sort Time Analysis (the best part)
Sorting an Array
Merge Sort Time Analysis (the best part)
n=8
4 4
2 2 2 2
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
n=8
4 4
2 2 2 2
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
n=8
4 4
2 2 2 2
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
4 4
2 2 2 2
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
4 4
2 2 2 2
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
2 2 2 2
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
1 1 1 1 1 1 1 1
Sorting an Array
Merge Sort Time Analysis (the best part)
Sorting an Array
Merge Sort Time Analysis (the best part)
Sorting an Array
Merge Sort Time Analysis (the best part)
Sorting an Array
Merge Sort Time Analysis (the best part)