-2
在我的代碼中,我正在處理我有一個二進制搜索,它是假設找到具體的數字,但現在我無法弄清楚爲什麼它告訴我沒有找到每一個數字。我正在嘗試使用遞歸。遞歸BinarySearch問題
public class BinarySearch {
private static boolean binarySearch(int[] myList, int numberToFind) {
// So this will be your recursive method.
// Right now it just returns false.
// But you need to change this code.
return false;
}
public static void main(String[] args) {
// Create an array of sorted numbers
int[] evenList =
{ 2, 4, 9, 11, 17, 19, 22, 29, 30, 33,
39, 43, 46, 47, 51, 52, 54, 56, 58, 59,
63, 69, 70, 79, 88, 89, 92, 96, 98, 99 };
// Can we find every number?
for (int i = evenList.length -1; i >= 0; i--) {
if (binarySearch(evenList, evenList[i]))
System.out.printf("%d was found.\n\n", evenList[i]);
else
System.out.printf("%d was not found.\n\n", evenList[i]);
}
// Will we not find these numbers?
int[] testCases = { 1, 44, 100, 32 };
for (int i = 0; i > testCases.length; i--) {
if (binarySearch(evenList, testCases[i]))
System.out.printf("%d was found.\n\n", testCases[i]);
else
System.out.printf("%d was not found.\n\n", testCases[i]);
}
}
}
我可以告訴你爲什麼它找不到任何數字...... –
閱讀包含註釋的代碼將是一個不錯的第一步。 –
我也可以。它在評論中。 –