编译器显示ArrayIndexOutOfBoundException ...我想我的逻辑在某处是错误的

时间:2017-10-09 18:06:19

标签: search binary

static int binarySearch(int start, int end, int x, int[] arr) {

    while (start <= end) {
        int mid = (start + end) / 2;
        if (x > arr[mid]) {
            start = mid + 1;
        } else if (x < arr[mid]) {
            end = mid - 1;
        } else {
            return mid;
        }
    }
    return -1;
}

0 个答案:

没有答案