在Java中测试二进制搜索的效率

在Java中测试二进制搜索的效率,java,arrays,algorithm,performance,binary-search,Java,Arrays,Algorithm,Performance,Binary Search,我试图在java中测试二进制搜索大型排序数组的时间效率 这是我正在使用的二进制搜索方法,它接受搜索键和数组 public int binarySearch(int[] a, int k) { int l = 0; int r = a.length - 1; int m = 0; while (l <= r) { m = (l+r)/2; if (k == a[m]) { return m;

我试图在java中测试二进制搜索大型排序数组的时间效率

这是我正在使用的二进制搜索方法,它接受搜索键和数组

public int binarySearch(int[] a, int k) {
    int l = 0;
    int r = a.length - 1;

    int m = 0;

    while (l <= r) {
        m = (l+r)/2;
        if (k == a[m]) {
            return m;
        }
        else if (k < a[m]) {
            r = m - 1;
        }
        else {
            l = m + 1;
        }
    }
    return -1;
}

实现这一点的最佳方法是什么?

如果数组大小太大,无法放入内存,则必须将数组分成数据块,然后在每个数据块中搜索该数据。@javafan谢谢。我了解如何实现该功能。但我试图从时间的角度分析二进制搜索方法的效率。因此,我希望向该方法发送一个数组,并计算时间效率。不要在编译时初始化它:在运行时初始化它。请注意@Zabuza非常感谢。这是有用的。似乎我将继续以编程方式填充数组方法。
public static int D10000[] = {0, 1, 2, 3, ..... 10000};

public static ArrayList<Integer> D10000 = new ArrayList<Integer>(Arrays.asList(1, 2, 3, 4, 5, ...... 10000);
Information:java: The system is out of resources. 
Information:java: Consult the following stack trace for details.
Information:java:   at com.sun.tools.javac.code.Type.map(Type.java:220)
Information:java: Errors occurred while compiling module 'SearchAlgo'
Information:javac 1.8.0_144 was used to compile java sources
Information:21/10/17, 12:02 PM - Compilation completed with 1 error and 0 warnings in 1s 481ms 
Error:java: java.lang.StackOverflowError