You are on page 1of 3

Recursion

Computer Science 2
2023-2024

1 Introduction
In algorithmics, recursion is when a function calls itself in its own code. It can be
seen as an embedding of calls to the same function. Obviously, this embedding
of calls must be associated with a stopping criterion.

1.1 Calculation of the factorial:


Consider the iterative algorithm that calculates the factorial of a certain integer
n:
i n t f a c t o r i a l ( i n t n){
i n t f =1;
i f ( n!=0 && n !=1){
f o r ( i n t i =2; i<=n ; i ++){
f=f ∗ i ;
}
}
return f ;
}
This algorithm correctly calculates the factorial of a number. However, it
does not take into account an important property of the function factorial :
recursion. We consider the sequence un = n!. This sequence is a recursive
sequence because un+1 = f (un ) = (n + 1) · un . Therefore, to calculate un+1 !, it
suffices to calculate un and then multiply it by n + 1. Therefore, if we know the
value of u0 , we can calculate un , ∀n ∈ IN without having to know the expression
of the sequence (un ).
This recursive relationship can be concretized by transforming the factorial
function into a recursive function:
i n t f a c t o r i a l ( i n t n){
i f ( n==1 | | n==0){
return 1;
} else {
r e t u r n ( n ∗ f a c t o r i a l ( n−1)) ;

1
}
}
In order to better visualize how the function calls itself several times until
it reaches the value 1, execute the following code:
#i n c l u d e <s t d i o . h>
i n t f a c t o r i a l ( i n t n){
i f ( n==1 | | n==0){
p r i n t f ( ” f u n c t i o n r e t u r n s 1\n ” ) ;
return 1;
} else {
p r i n t f ( ” f u n c t i o n r e t u r n s %d ∗ f a c t o r i a l (%d −1) = %d ∗
%d\n ” , n , n , n , f a c t o r i a l ( n − 1 ) ) ;
r e t u r n ( n ∗ f a c t o r i a l ( n−1)) ;
}
}

2 Dichotomy search
Dichotomy search is an algorithm that aims to optimize the search for a certain
value in a sorted array. The algorithm consists of comparing the searched value
with the value of the cell that is in the middle of the array. If the searched
value is greater than the value in the middle, then the searched value, if it exists
in the array, is in the first half of the array. Otherwise, the searched value, if
it exists in the array, is in the second half of the array. We reapply the same
method but this time considering only the half of the array chosen. And so on
until the searched value is found, or until we reach an array that contains only
one cell.
#i n c l u d e <s t d i o . h>

i n t d i c h o t o m y s e a r c h ( i n t ∗ tab , i n t s t a r t i n d e x , i n t e n d i n d e x , i n t x ) {
i f ( s t a r t i n d e x == e n d i n d e x ) {
i f ( tab [ s t a r t i n d e x ] == x ) {
return start index ;
} else {
r e t u r n −1; // not found
}
} else {
int i middle = ( s t a r t i n d e x + end index ) / 2;
i f ( tab [ i m i d d l e ] > x ) {
r e t u r n d i c h o t o m y s e a r c h ( tab , s t a r t i n d e x , i m i d d l e − 1 , x ) ;
} e l s e i f ( tab [ i m i d d l e ] < x ) {
r e t u r n d i c h o t o m y s e a r c h ( tab , i m i d d l e + 1 , e n d i n d e x , x ) ;
} else {

2
return i middle ;
}
}
}

i n t main ( ) {
i n t tab [ 1 0 ] ;
// The u s e r f i l l s t h e a r r a y
f o r ( i n t i = 0 ; i < 1 0 ; i ++) {
p r i n t f ( ” P l e a s e e n t e r t h e v a l u e f o r c e l l %d\n ” , i ) ;
s c a n f (”%d\n ” , &tab [ i ] ) ;
}
int val ;
p r i n t f ( ” P l e a s e e n t e r t h e v a l u e t o s e a r c h f o r i n t h e a r r a y \n ” ) ;
s c a n f (”%d\n ” , &v a l ) ;
i n t i v a l = d i c h o t o m y s e a r c h ( tab , 0 , 9 , v a l ) ;
i f ( i v a l == −1) {
p r i n t f ( ” \ nValue not found i n t h i s a r r a y \n ” ) ;
} else {
p r i n t f ( ” \ nThe v a l u e %d i s found i n c e l l %d\n ” , val , i v a l ) ;
}

return 0;
}

You might also like