我感覺非常非常愚蠢,因爲我解決了比這更難的東西。 這應該是有序二分搜索的實現。每當我跟蹤12,彈出一個stackoverflow錯誤。請幫忙嗎?執行二進制搜索的問題
public class binarySearch {
public static void main(String[] args) {
int[] arr = { 1, 5, 6, 8, 12, 88 };
System.out.println(binaryHelper(0, arr.length - 1, 12, arr));
}
public static int binaryHelper(int first, int last, int target, int[] arr) {
if(first > last) return -1;
else {
int mid = first + last/2;
if(arr[mid] == target) return mid;
else if(arr[mid] > target) return binaryHelper(first, mid - 1, target, arr);
else return binaryHelper(mid + 1, last, target, arr);
}
}
}
[Java BinarySearch]的可能重複(http://stackoverflow.com/questions/12517764/java-binarysearch) – Joe 2014-09-13 11:24:35
您認爲'first + last/2;'是如何評估的? – 2014-09-13 11:30:32