In: Computer Science
JAVA:
By comparing and contrasting recursive vs iterative algorithms.“How does the Sum_Recursive() function work and How does each recursive call reduce the problem?
The Code:
public static void main(String[] args) {
int value, result;
value = 9;
result = Factorial_Iterative(value);
System.out.println("Factorial of " + value + " using (Iterative
Calculation) is: " + result);
result = Factorial_Recursive(value);
System.out.println("Factorial of " + value + " using (Recursive
Calculation) is: " + result);
int theArray[] = {9, 3, 2, 4};
result = Sum_Iterative(theArray, theArray.length);
System.out.println("Sum of array elements { 9, 3, 2, 4 }:" + "using
(Iterative Calculation) is: " + result);
result = Sum_Recursive(theArray, theArray.length);
System.out.println("Sum of array elements { 9, 3, 2, 4 }:" + "using
(Recursive Calculation) is: " + result);
}
public static int Factorial_Iterative(int n) {
int result = 1;
for (int i = n; i >= 1; i--) {
result = result * i;
}
return (result);
}
public static int Factorial_Recursive(int n) {
System.out.println(n);
if (n == 0) // base case
{
return 1;
} else {
return n * Factorial_Recursive(n - 1);
}
}
//--------------------------------------------------------------
public static int Sum_Iterative(int array[], int ArraySize)
{
int result = 0;
for (int i = 0; i < ArraySize; i++) {
result = result + array[i];
}
return (result);
}
//--------------------------------------------------------------
public static int Sum_Recursive(int array[], int ArraySize)
{
if (ArraySize == 1) {
return (array[0]);
} else {
return (array[ArraySize - 1] + Sum_Recursive(array, ArraySize -
1));
}
}
}
First of all what is recursive function:
A function which is being called by iteslf, or a calling to same function inside the same function body.
So, Sum_Recursive function is a recursive function because it calls to itself at line:
return (array[ArraySize - 1] + Sum_Recursive(array, ArraySize - 1));
so, what does this line means and how its working to calculate sum=18.
Explanation:
At first call when Sum_Recursive called by main function:
array[]: {9, 3, 2, 4} arraySize: 4
if,
inside the function we check is arraySize is equals to 1, if it is then retun array[0]=9 but at first call arraySize is 4 so this will not execute the if condition.
else,
it returns array[arraySize-1] which is =4 plus Sum_Recursive(array, ArraySize-1), so here calling again to sum_recursive without returning the result yet, so result will be (4 + Sum_Recursive(array, ArraySize - 1)).
then calling will again occur the if condition will not execute because array size 3 so in elese part, the previouse result 4 and this time return array[arraySize-1] which is =3 so result will be (4+3+Sum_Recursive(array, ArraySize - 1)).
again calling if condition will not execute because arraySize = 2
so else part will run and result will be (4+3+2 +Sum_Recursive(array, ArraySize - 1)) and dont forget sum_recursive is calling again to same function.
now arraySize will be 1 if condtion will true and it returns array[0] which is 9
so, result will result = (4+3+2 +9) = 18
which is the last recursive call and whole sum 18 will be returned to main function.
//OUTPUT//
Comment down for any queries!
Please give a thumbs up if you are satisfied and helped with the
answer :)