0% found this document useful (0 votes)
48 views9 pages

Computer Science: Recursion

Recursion is a programming technique where a function calls itself during its execution. There must be a base case or condition to stop the recursion, and each recursive call must move closer to the base case. Examples of recursion include calculating factorials, Fibonacci numbers, and binary search of a sorted list. Recursive functions define the problem in terms of simpler versions of the same problem until a base case is reached.

Uploaded by

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

Computer Science: Recursion

Recursion is a programming technique where a function calls itself during its execution. There must be a base case or condition to stop the recursion, and each recursive call must move closer to the base case. Examples of recursion include calculating factorials, Fibonacci numbers, and binary search of a sorted list. Recursive functions define the problem in terms of simpler versions of the same problem until a base case is reached.

Uploaded by

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

Chapter 5 :

Computer Science
Class XII ( As per
CBSE Board)
Recursion

Visit : python.mykvs.in for regular updates


Recursion
It is a way of programming or coding technique, in which a function
calls itself for one or more times in its body. Usually, it is returning
the return value of this function call procedure. If a function
definition fulfils such conditions, we can call this function a
recursive function.

Visit : python.mykvs.in for regular updates


Recursion
The Two Laws of Recursion
• Must have a base case - There must be at
least one base criteria/condition, when
such condition is met the function stops
calling itself.
• Must move toward the base case - The
recursive calls should moves in such a
way that each time it comes closer to the
base criteria.

Visit : python.mykvs.in for regular updates


Recursion
Factorial of a Number Using Recursion
ALGORITHM
1.Test if n <= 0. If so,
return 1. (factorial 5)
(* 5 (factorial 4))
(* 5 (* 4 (factorial 3)))
2.If not, then call the (* 5 (* 4 (* 3 (factorial 2))))
(* 5 (* 4 (* 3 (* 2 (factorial 1)))))
factorial algorithm (* 5 (* 4 (* 3 (* 2 1))))
with n – 1 and (* 5 (* 4 (* 3 2 )))
(* 5 (* 4 6 ))
multiply the result by (* 5 24 ))
n and return that 120

value.

Visit : python.mykvs.in for regular updates


Recursion
Factorial of a Number Using Recursion
PYTHON PROGRAM
def factorial(x):
if x==1: (factorial 5)
(* 5 (factorial 4))
return 1 (* 5 (* 4 (factorial 3)))
else: (* 5 (* 4 (* 3 (factorial 2))))
(* 5 (* 4 (* 3 (* 2 (factorial 1)))))
return x*factorial(x-1) (* 5 (* 4 (* 3 (* 2 1))))
(* 5 (* 4 (* 3 2 )))
(* 5 (* 4 6 ))
f=factorial(5) (* 5 24 ))
print ("factorial of 5 is ",f) 120

Visit : python.mykvs.in for regular updates


Recursion
Fibonacci numbers Using Recursion
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …
Algorithm

Fib(n)
1. If n =1 or n=2, then
2. return 1
3. Else
4. a = Fib(n-1)
5. b = Fib(n-2)
6. return a+b

Visit : python.mykvs.in for regular updates


Recursion
Fibonacci numbers Using Recursion
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …
Program
def fib(n):
if n <= 1:
return n
else:
return(fib(n-1) + fib(n-2))

nterms = int(input("enter a number"))

if nterms <= 0:
print("Plese enter a positive integer")
else:
print("Fibonacci sequence:")
for i in range(nterms):
print(fib(i))

Visit : python.mykvs.in for regular updates


Recursion
Binary Search Using Recursion
Algorithm
1. Find the midpoint of the array; this will be the element
at arr[size/2]. The midpoint divides the array into two
smaller arrays: lower half and upper half
2. Compare key to arr[midpoint] by calling the user
function cmp_proc.
3. If the key is a match, return arr[midpoint]; otherwise
4. If the array consists of only one element return NULL,
indicating that there is no match; otherwise
5. If the key is less than the value extracted from
arr[midpoint] search the lower half of the array by
recursively calling search; otherwise
6. Search the upper half of the array by recursively
calling search.
NOTE:- For binary search all elements must be in order.

Visit : python.mykvs.in for regular updates


Recursion
Binary Search Using Recursion
Program
def binsrch(alist, item):
first = 0
last = len(alist)-1
found = False
while first<=last and not found:
midpoint = (first + last)//2
if alist[midpoint] == item:
found = True
else:
if item < alist[midpoint]:
last = midpoint-1
else:
first = midpoint+1
return found
testlist = [1,3,5,6,7,8,10,13,14]
print(binsrch(testlist, 2))
print(binsrch(testlist, 5))
Visit : python.mykvs.in for regular updates

You might also like