简体   繁体   中英

Why does the method return a different value if its arguments are previously set variables rather than inputed directly?

Below is the relevant part of my program.

Relevant part of main method

    int[] C = createIntArray_descending();
    int found, target, from, to;
    int[] array;

    array = C;
    target = 5;
    from = array[0];
    to = array[array.length-1];
    printArray(array);
    bubble_sort(array);
    printArray(array);
    found = searchAscending(target, array, from, to);
    found = searchAscending(5, C, C[0], C[C.length-1]);
    System.out.println(found);

Method searchAscending(), a binary search method

 public static int searchAscending(int target, int[] array, int from, int to) {
    int mid = (from + to)/2;
    if (to<from)
        return -1; 
    else if (target == array[mid]) 
        return mid;
    else if (to == from) 
        return -1;
    else if (target < array[mid])
        return searchAscending(target, array, from, mid-1);
    else
        return searchAscending(target, array, mid+1, to);
}

When I run the program using

found = searchAscending(5, C, C[0], C[C.length-1]);

the method returns 4, meaning that 5 is in fact in the array C, found at position 4. I then wanted to generalise it for other arrays, so I created variables target, from, to and an empty int array, which I could then set as whatever array I wanted to apply the method to, using

found = searchAscending(target, array, from, to);

However when I run the program using this line rather than the previous one, the method returns -1 (meaning it didn't find the target, 5, in the array).

Why are they returning different values? They seem to be essentially the same to me.

EDIT:

I figured out that the problem comes from the variables from and to, the the line found = searchAscending(target, array, array[0], array[array.length-1]) works, so the values are successfully being passed, but (target, array, from, array[array.length-1]) gives an ArrayIndexOutOfBoundsException....

So I get the same answer no matter which way I call it. However I did notice a problem with how you call your binary search function, searchAscending. For to and from you all calling it using the value at the index, rather than the index itself. Try this for your main:

int[] C = createIntArray_descending();
int found, target, from, to;
int[] array;

array = C;
target = 5;
from = 0;
to = array.length-1;
printArray(array);
bubble_sort(array);
printArray(array);

found = searchAscending(target, array, from, to);
System.out.println(found);
found = searchAscending(5, C, 0, C.length-1);
System.out.println(found);

First, I'm going to assume createIntArray_descending() creates array {5,4,3,2,1} , and bubble_sort(array) correctly sorts that to {1,2,3,4,5} .

Your main problem is that from and to are indexes , but you pass in values from the array.

Also note that array = C is meaningless to your code, because it does not copy the array. array and C are referencing the same array.

To explain why it doesn't work, use a debugger and examine the values of your code:

int[] C = createIntArray_descending();              // C = {5,4,3,2,1}

array = C;                                          // array = {5,4,3,2,1}
target = 5;
from = array[0];                                    // from = 5
to = array[array.length-1];                         // to = 1
printArray(array);
bubble_sort(array);                                 // array = C = {1,2,3,4,5}
printArray(array);
found = searchAscending(target, array, from, to);   // (5, C, 5, 1) -> found = -1
found = searchAscending(5, C, C[0], C[C.length-1]); // (5, C, 1, 5) -> found = 4
System.out.println(found);

In both calls to searchAscending() , you were supposed to call with from = 0 and to = 4 . Both calls are incorrect.

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