I'm trying to understand recursion by doing some codes. This code should find the maximum number in the array recursively, but for some reason it doesn't work. Thanks for your help!
public static void main(String[] args) {
int array [] = {11,4,6,9,15,2};
maxNum(array, 1, 0);
}//Main
public static int maxNum(int array[], int maxIndex, int i)
{
int ans;
if(i==array.length-1) //Condition Stop
{
ans= maxIndex;
}
else
{
ans= maxNum(array, Math.max(array[maxIndex], array[i]), i++);
}
System.out.println(array[maxIndex]);
return ans;
}//maxNum
}//Class
-
isn't by using the Math.max(x ,y) ?user3075653– user30756532014年01月16日 13:38:32 +00:00Commented Jan 16, 2014 at 13:38
-
Recursion is usually done by calling the method again with a subset of the data. Like if you had a directory structure, you'd say "give me the biggest file in c:\". Then you would say "give me the biggest file in "c:\userfolder1\", then for each folder in userfolder1, you'd do the same. It breaks down the problem into smaller chunks until it reaches a simple end condition. It looks like you're trying to re-code a for loop using recursion, which is missing the point of recursion a little.Kieveli– Kieveli2014年01月16日 13:39:39 +00:00Commented Jan 16, 2014 at 13:39
9 Answers 9
I see a problem with the bit Math.max(array[maxIndex], array[i])
. You are using this as the second argument for the recursive call to maxNum()
, but you are getting a value from the array, rather than an index. In other words, you're ending up using some value like 11 or 15 from your array as an index.
Comments
public static void main(String[] args) {
int array [] = {11,4,6,9,15,2};
System.out.println(maxNum(array, 0, 1));
}//Main
public static int maxNum(int array[], int maxIndex, int i)
{
int ans;
if(i==array.length) //Condition Stop
ans=maxIndex;
else
ans=maxNum(array, (array[maxIndex]<array[i])?i:maxIndex, ++i);
return ans;
}//maxNum
}//Class
two modifications: i++ -> ++i and Math.max(array[maxIndex], array[i]) -> (array[maxIndex]
Comments
The issue with your code is that Math.max() is returning the value 11 and you are trying to pass it as maxIndex to your second call of maxNum. This is resulting into trying to access array[11] in the next call, which obviously doesn't exist and the code is resulting into ArrayOutOfBoundsException
. Another issue is that you are using a post increment for i which means, i value will be incremented always after the value is passed to maxNum. You should be changing it to ++i.
Instead of maxNum(array, Math.max(array[maxIndex], array[i]), i++);
use
maxNum(array, array[maxIndex]>array[i]?maxIndex:i, ++i);
The above code will pass maxIndex or i to the next call, which will be either 0 or 1 in first recursive call to maxNum.
This code doesn't fix all your issues with code. You are printing array[maxIndex]
which may result printing it many times. I will leave that to you to fix.
1 Comment
In this line of code first time your doing something legal,trying to access index which is present in the array.But next iteration you are passing 11 as index because you find this value as the maximum,change your logic for that.
ans=maxNum(array, (array[maxIndex]<array[i])?i:maxIndex, ++i);
And this 11 index is not present in the array so you are getting this error.Next time you can try debugging your code through your IDE. This code requires debugging only.
Comments
You can use the following code:
public static main(String ar[]){
int array [] = {11,4,6,9,15,2};
System.out.println(array[maxNum(array, 0, 0)]);
}
public static int maxNum(int array[], int maxIndex, int i)
{
int ans=0;
if(i==array.length-1) {
ans= maxIndex;
}
else {
ans= maxNum(array, (Math.max(array[maxIndex], array[i])==array[maxIndex])?maxIndex:i, ++i);
}
return ans;
}
Problem with your code :
maxNum
call was passing value , instead of index .
And ++i
and i++
are different .
In function call , if you want to pass incremented value of i
, use ++i
.
Comments
There is a problem in this line:
ans= maxNum(array, Math.max(array[maxIndex], array[i]), i++);
The second argument expects an array index. However you call it with an array value:
Math.max(array[maxIndex], array[i])
return a value from the array not an index.
Even when you fix this there is really no added value to recursion here. The recursive call simply looks up the next value in the array. All you are really doing is simply iterating over the array
However if instead you built the findMax function to follow a logic like this:
- Split the array in half call findMax for each half array
- Compare the results return the real maximum.
the recursion feels less superficial
Comments
In this line Math.max(array[maxIndex], array[i]) you should return the next index and not the max value found. If you return the max value, you won ́t be making pair comparisons as I assume you want.
Comments
Recursion is an intricate subject. For a while a tried to solve every single problem with recursion, which is not always a good idea.
Like many already pointed out (@hasan, @Buddha, etc), in the provided example code you're mixing "values in the array" with "indexes in the array". If you're already trying to understand recursion, might as well revisit arrays.
Good approach is trying to simplify your code, e.g., you can eliminate variable maxIndex
and rely only on i
and i+1
. This will make your code less error prone as you have less things to take into account.
Comments
try this
public static void main(String[] args) {
int array [] = {11,4,6,9,15,2};
System.out.println(maxNum(array, array[0]));
}
public static int maxNum(int array[], int max)
{
if(array.length == 0)
return max;
return maxNum(Arrays.copyOfRange(array, 1, array.length), Math.max(array[0], max));
}