简体   繁体   中英

binary search on array of integer in java

I know this is a very famous question and lots of answers provided already, but I am trying to implement binary search algorithm in java of my own.

First of all getting following compilation error, why??

This method must return a result of type int

Second how this approach differs from this famous solution

public static int binarySearch(int test[], int num){

    int midLength = test.length/2;
    int fullLength = test.length;

    if(num > test[midLength]){
        int newArray[] = new int[fullLength - midLength];
        for (int j = 0; j<= midLength ; j++){
            newArray[j] = test[midLength + j];

        }
        return binarySearch(newArray, num);
    }
    else if(num < test[midLength]){
        int newArray[] = new int[midLength];
        for (int j = 0; j<= fullLength - midLength ; j++){
            newArray[j] = test[j];

        }
        return binarySearch(newArray, num);
    }
    else if(num == test[midLength]){
        return test[midLength];
    }

}

public static void main(String[] args) {
    int test[] = {2,8,1,6,4,6};
    Arrays.sort(test);
    int num = ArraysTest.binarySearch(test, 1);
    System.out.println(num);
}

Please ignore the boundary conditions and logical mistakes, as this is draft version.

There is a missing return at the end of your binarySearch function. In Java, the compiler verifies that on every possible execution path a return of the right type exists. In your case, if all tests are false, then the execution rises the end of the function where there is no returned value, violating the function contract.

Your algorithm differs from the cited one in the way that yours constructs a new array at each ''split''. So, we can say that it is relatively inefficient because you use too much memory without any real need for it.

You don't have an "else" clause in your binarySearch() method. This fall-thru case can be done by adding a return statement at the end of the method. In other words, the "else" need not to be explicit. The compiler thinks that there is a possibility that none of the tests (if and else-ifs) will pass, so the method returns nothing. Also, since the method is recursive, it MUST have a default (escape) clause. Otherwise, it will call itself forever. So, just remove the else-if surrounding return test[midLength]; .

Jean is right you are not returning any values in the last in your function so what if not any if condition and else-if condition got true.

Second thing when you use else-if you must provide else condition in last so that code doesn't fails anyways.

    int midLength = test.length/2;
    int fullLength = test.length;

    if(num > test[midLength]){
        int newArray[] = new int[fullLength - midLength];
        for (int j = 0; j<= midLength ; j++){
            newArray[j] = test[midLength + j];

        }
        return binarySearch(newArray, num);
    }
    else if(num < test[midLength]){
        int newArray[] = new int[midLength];
        for (int j = 0; j<= fullLength - midLength ; j++){
            newArray[j] = test[j];

        }
        return binarySearch(newArray, num);
    }
    else if(num == test[midLength]){
        return test[midLength];
    }

    //if it is not matched with any conditions above.
    else{
        return 0;
    }

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