binary search on array of integer in java

非 Y 不嫁゛ 提交于 2020-01-16 18:11:09

问题


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.


回答1:


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.




回答2:


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];.




回答3:


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;
    }


来源:https://stackoverflow.com/questions/28099177/binary-search-on-array-of-integer-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!