0% found this document useful (0 votes)
11 views20 pages

12. Lecture

Uploaded by

somanfatima33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views20 pages

12. Lecture

Uploaded by

somanfatima33
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

CSC103: Introduction to Computer and

Programming
Lecture No

[email protected] 1
Recursion
What is recursion?

Recursion
See “Recursion”.

Recursion
If you still don't get it, see "Recursion"

2
Recursion
What is recursion?

The process in which a function calls itself is known as recursion


and the corresponding function is called the recursive
function.

3
Recursion
Void fun()

{
cout<<“this is fun”<<endl;
fun();
}

4
Recursion

Void fun()
{
cout<<“this is fun”<<endl;
fun();
} When will this end?

Example of program to print numbers in descending order?

5
Recursion
Example of program to print numbers in descending order?
public static int print(int n) {
System.out.println(n); // Print the current value of n
if (n > 1)
{
print(--n); // Recursive call with n-1 (decrement n before passing)
}
else
{
return 1; // Base case: return 1 when n reaches 1
}
}
public static void main(String[] args) {
print(10); // Start the recursion by calling the print function with 10
}

6
Properties of recursive functions
1. Base Case(s): condition for terminating the recursive process
2. Recursive Case(s): set of rules to break the problem into smaller
sub-problems to reach base case
a) Divide the problem into smaller sub-problems
b) Solve the sub-problems
c) Combine results to get answer

7
Need of Base Case and Recursive Case

int loop(int x) Recursive function with


{
– no base case
return (1 + loop(x))
} – not a valid recursive case

• Trace Table with x=5


Problem not being divided into
loop 5 smaller problems – no
termination

1 + loop 5

1 + loop 5
infinite loop – no
… termination
8
8
Power function
• Lets figure out a recursive function for calculating
the Powers of a given number

2nd power function


Square of x = x*x

3rd power function


Cube of x = x*x*x

4th power function


Fourth Power of x = x*x*x*x

9
Power function

x4 = x*x*x*x = x*( x*x*x ) = x*x3


x5 = x*x*x*x*x = x*(x*x*x*x ) = x*x4
x6 = x*x*x*x*x*x = x*(x*x*x*x*x ) = x*x5
In general

xn = x*xn-1
public static int power (int x, int n)
{
return x * power (x, n-1)
}

10
Power Function
public static int power (int x, int n)
{
return x * power (x, n-1)
Trace table
}

Calc 23: Power (2,3) x * power (x, n-1)


Step no.
x=2, n=3
We know 20=1
1 2* power(2,2) Base case: if n==0 return 1
We need to stop here
2 2* 2* power(2,1)
When does it stop ?
3 2*2* 2*power(2,0)

4 2*2*2* 2*power(2,-1) 11
11
Revised Power Function
public static int power (int x, int Base
n) case Trace table: Calc 23: x=2, n=3
{
If (n==0)
return 1; Power (2,3) =8
else 2* power(2,2)
4
2
2* 2* power(2,1)
return x * power (x, n-1)
2*2* 2*power(2,0)
1
}

Recursive case Result: 8

sub-problems must be “smaller” than the


original problem otherwise the recursion
never terminates.

12
12
Factorial function

Factorial 0! = 1
1! = 1
2! = 2 * 1 = 2
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24

13
Factorial function
0! = 1 1*0!
1! = 1 2*1!
2! = 2 * 1 = 2
3*2!
3! = 3 * 2 * 1 = 6
4! = 4 * 3 * 2 * 1 = 24 4*3!

……

In general: n!=n*(n-1)!

Recursive case: Base case:


Factorial(n)=n*factorial(n-1) 0!=1 i.e; if (n==0) return 1

14
Factorial function
Int factorial (int n)
Trace table: Calc 4! here n=4
{
If (n==0)
factorial (4) =24
return 1;
else 4* factorial
6 (3)
return n * factorial (n-1) 4* 3* factorial
2 (2)
} 4*3* 2* factorial
1 (1)
4*3*2* 1* factorial
1 (0)

15
Factorial function
Version Action Argument or Return Value
1 Call 5
2 Call 4
3 Call 3
4 Call 2
5 Call 1
5 Return 1
4 Return 2
3 Return 6
2 Return 24
1 Return 120

16
Fibonacci sequence
The first ten terms in the sequence are:
1,1,2,3,5,8,13,21,34,55

Each value, except for first two, is sum of last two values

Simply saying:
Fib(n)= fib(n-1)+fib(n-2) except for when n=0 and n=1

Base case: Recursive case:


if (n==0 or n==1) Fib(n)= fib(n-1)+fib(n-2)
Return 1

17
Function for fibonacci sequence
Int fib (int n)
{
If (n==0 or n==1)
return 1;
else
return fib(n-1) +fib (n-2);
}

18
Trace of Fibonacci(5)
If (n==0 or n==1)
return 1; fib 5 =8
else
return fib(n-1) +fib (n-2);

5 3
fib 4 + fib 3

3 fib 3 + fib 2 2 1
2fib 2 + fib 1

2 fib 2 + fib 1 fib 1 + fib 0 fib 1 + fib 0


1 1
1 1
1

1 fib 1 + fib 0 1

19
Why recursion?
• Recursion makes the • Recursion makes
program faster? the code much
• Recursion uses less simpler and Easy
memory? to read

20

You might also like