我的二分查找有什么问题?

时间:2014-01-25 00:46:54

标签: java binary-search

我刚刚开始学习Java(这是我第一次编程)。如果print语句(纯粹用于测试目的),代码会反复输出mid而不会更改它。我已经考虑了好几个小时了,无法弄明白。非常感谢帮助。

/*class containing binary search algorithm*/
public class BinarySearch {
    /*conducts a binary search as specified by user*/
    public static int binarySearch(int queryValue, int[] list) {
    int length = list.length; 
    /*last point of list*/
    int top = length-1;       
    /*first point of list*/
    int bottom = 0;    
    /*starting midpoint of list*/
    int mid = (int)Math.round((top + bottom)/2);
    /*binary search*/
    while(bottom < top) {
        if((int)queryValue == (int)list[mid]) {
        return mid;
        }
        else if(queryValue > list[mid]) {
         bottom = mid;
         mid = (int)Math.round((top + bottom) / 2);
         StdOut.print(mid);
        }
        else {
         top = mid;
         mid = (top + bottom) / 2;
         }
    }
    /*returns -1 if user value not found*/
    return -1;
    }
}

3 个答案:

答案 0 :(得分:3)

如果您的值大于中点,则消除中点。将bottom提前一个mid

bottom = mid + 1;

同样,对于少于中点的情况,请在当前top之前提前mid

top = mid - 1;

否则,您可能会遇到bottomtop从未相互交叉的情况。

此外,二进制搜索仅在输入已排序时才有效。请确认/确保您的阵列已经排序。

答案 1 :(得分:0)

如果您提供用于测试的数据会更好 至于现在,我看到以下输入它会挂起:
binarySearch(9, new int[] {1,2,3,4,5,6,7,8,9})
中期将是7
这是因为(7 + 8)/ 2 = 7,因为你使用int
尝试更换:
mid = (int)Math.round((top + bottom) / 2);

mid = (int)Math.round((top + bottom) / 2.0);
它将解决问题 祝好运!
更新了代码以考虑边缘情况:

public static int binarySearch(int queryValue, int[] list) {
    int length = list.length;
    /*last point of list*/
    int top = length-1;
    /*first point of list*/
    int bottom = 0;
    /*starting midpoint of list*/
    int mid = (int)Math.round((top + bottom)/2.0);
    /*binary search*/
    do {
        if (queryValue > list[mid]) {
            bottom = mid;
            mid = (int)Math.ceil((top + bottom) / 2.0);
            System.out.println(mid);
        } else {
            top = mid;
            mid = (int)Math.floor((top + bottom) / 2.0);
            System.out.println(mid);
        }
        if(queryValue == list[mid]) {
            return mid;
        }
    } while (mid < top && mid > bottom);
    /*returns -1 if user value not found*/
    return -1;
}

答案 2 :(得分:0)

public static int binarySearch(int queryValue, int[] list) {
    int length = list.length;
    /*last point of list*/
    int top = length-1;
    /*first point of list*/
    int bottom = 0;
    /*starting midpoint of list*/
    int mid = (int)Math.round((top + bottom)/2.0);
    /*binary search*/
    do {
        if (queryValue > list[mid]) {
            bottom = mid;
            mid = (int)Math.ceil((top + bottom) / 2.0);
            System.out.println(mid);
        } else {
            top = mid;
            mid = (int)Math.floor((top + bottom) / 2.0);
            System.out.println(mid);
        }
        if(queryValue == list[mid]) {
            return mid;
        }
    } while (mid < top || mid > bottom);
    /*returns -1 if user value not found*/
    return -1;
}