简体   繁体   中英

How do I solve this using recursion in java?

The rules are that I have to return the largest number, so pretty much the user wants to find the biggest number. Split the array in half or close to it and find the biggest value from the array in the lower half and find the biggest value in the high half. I just started learning recursion and this problem is giving me nightmares.

public static double getBiggest(double[] a, int low, int high)
{    
    int mid = (low+high)/2;
    double one = 0;
    double two = 0;       
    if(a == null || a.length == 0 || low > high)
        throw new IllegalArgumentException();

    //How do I loop to find the biggest?
    //A way to narrow down a big number of array values into a single one

    one = getBiggest(a,low,mid);    
    two = getBiggest(a,mid,high);

    if(one >= two)
        return one;
    else
        return two;

  return -1;
}

Your code is already pretty close, but since getBiggest always calls getBiggest(), it will never stop.

Before the recursive call, do something like this

if (low >= high)
{
    return a[low];
}

That still leaves a little problem: When high == low+1, mid == low, so getBiggest(a,mid,high) will be the same size as the call you're in. Recursing with the same arguments again means that it will never stop.

Your recursive calls should be like:

one = getBiggest(a,low,mid);    
two = getBiggest(a,mid+1,high);

That way both of the recursive calls are guaranteed to be smaller.

Finally, the conditional part at the end works, but the return a[mid] never happens. You should rewrite it like:

if(one >= two)
    return one;
else
    return two;       

In English, the algorithm works like this:

  1. If there's only one element, return it. Otherwise;
  2. Get the biggest element in the first half (the first recursive call), then
  3. Get the biggest element in the second half (the second recursive call), then
  4. return whichever is bigger.

A recursive solution to a problem is made from solutions to smaller versions of the same problem.

Any recursive algorithm must ALWAYS have a base case - if the problem is small enough that it can be solved trivially then that must be done instead. The function must not invariably call itself recursively, otherwise you are guaranteed to get infinite recursion, which in Java means a StackOverflowException . In this case you always call getBiggest as long as the arguments are valid so that is your problem. You wrote "If the low to high section is 3 or bigger" but there is no such if statement in your code. Adding one will allow you to avoid recursion when low and high meet.

Your implementation has two problems:

  • It is not doing the arguments checking correctly - the check needs to happen at the top, and
  • You are missing the base case. Any recursive program must specify what to do when you are down to one or two items; your program is not doing it.

You need to add a condition at the top to see if high minus low is two or less. If it is one, return the number at the low end; if there are two numbers, pick the max and return it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM