递归二进制搜索问题

时间:2015-05-01 05:05:06

标签: 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 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]);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

请查看此代码

splitAt

你需要在它工作之前实现该方法。