Recursion 递归二进制搜索问题
在我正在编写的代码中,我有一个二进制搜索,假设它能找到特定的数字,但现在我不明白为什么它告诉我没有找到每个数字。我正在尝试使用递归Recursion 递归二进制搜索问题,recursion,binary-search,Recursion,Binary Search,在我正在编写的代码中,我有一个二进制搜索,假设它能找到特定的数字,但现在我不明白为什么它告诉我没有找到每个数字。我正在尝试使用递归 public class BinarySearch { private static boolean binarySearch(int[] myList, int numberToFind) { // So this will be your recursive method. // Right now it just re
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]);
}
}
}
那么看看这个代码
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;
您需要先实现该方法,然后它才能工作。我可以告诉您为什么它找不到任何数字……阅读包含注释的代码将是一个很好的第一步。我也可以。在评论中。谢谢伙计们抱歉伙计们我很感激我有时会忘记它。for(int i=0;i