簡體   English   中英

我的二分查找有什么問題?

[英]What's wrong with my binary search?

我剛剛開始學習Java(這是我第一次學習Java)。 在print語句所在的位置(純粹出於測試目的),代碼會重復輸出中間值,而無需進行更改。 我已經考慮了幾個小時,無法解決。 幫助將不勝感激。

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

如果您的值大於中點,則消除中點。 在當前mid前移bottom 1:

bottom = mid + 1;

同樣,對於比中點情況較少,推進top目前前一個mid

top = mid - 1;

否則,您可能會遇到bottomtop永不交叉的情況。

同樣,二進制搜索僅在輸入已經排序時才起作用。 請確認/確保您的數組已經排序。

如果您提供用於測試的數據,那就更好了。
到目前為止,我看到以下輸入將掛起:
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;
}
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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM