Lecture 5 Recursion - Simple Sorting Techniques
Lecture 5 Recursion - Simple Sorting Techniques
Binary Recursion
Ex.1 : Summing the elements of a sequence using binary recursion
Mutual Recursion
int triangle(int n)
{
int total = 0;
while(n > 0) // until n is 1
{
total = total + n; // add n (column height) to total
--n; // decrement column height
}
return total;
}
int triangle(int n)
{
if(n==1)
return 1; //The condition that leads to a
else recursive method returning without
making another
return( n + triangle(n-1) ); recursive call is referred to as the
} base case. It’s critical that every
recursive method have a base case
to prevent infinite recursion
int factorial(int n)
{
if(n==0)
return 1;
else
return (n * factorial(n-1) );
}