Java 递归二进制搜索代码中的堆栈溢出错误

Java 递归二进制搜索代码中的堆栈溢出错误,java,search,Java,Search,我为“递归二进制搜索”编写了这段代码。我在标记行得到一个StackOverflowError。但是,我没有使用(l+h)/2作为mid,这可能会导致大值的堆栈溢出 public class RecursiveMethod { public static int RecursiveBinarySearch(int[] array, int l, int h, int x) { l = 0; h = array.length - 1;

我为“递归二进制搜索”编写了这段代码。我在标记行得到一个
StackOverflowError
。但是,我没有使用
(l+h)/2
作为
mid
,这可能会导致大值的堆栈溢出

public class RecursiveMethod
{
    public static int RecursiveBinarySearch(int[] array, int l, int h, int x)
    {
        l = 0;
        h = array.length - 1;

        while (l<=h)
        {
            int mid = l + (h-l)/2;

            if (x == array[mid])
                return mid;
            else if (x < array[mid])
                return RecursiveBinarySearch(array, l, mid-1, x); // overflow here
            else // last possibility: x > array[mid]
                return RecursiveBinarySearch(array, mid+1, h, x);
        }
        return -1;
    }

    public static void main (String[] args)
    {
        int[] a =
                  {2, 8, 12, 14, 16, 19, 24, 28, 31, 33,    // 0 - 9
                   39, 40, 45, 49, 51, 53, 54, 56, 57, 60,  // 10-19
                   63, 69, 77, 82, 88, 89, 94, 96, 97};     // 20-28

        for (int i = 0; i < a.length; i++)
            System.out.print("(" + a + "," + a[i] + ")" + "  ");

        System.out.println();
        System.out.print(RecursiveBinarySearch(a, 0, a.length, 1) + " ");
        System.out.print(RecursiveBinarySearch(a, 0, a.length, 26) + " ");
        System.out.print(RecursiveBinarySearch(a, 0, a.length, 85) + " ");
        System.out.print(RecursiveBinarySearch(a, 0, a.length, 99) + " ");
        System.out.println();
    }    
}
公共类递归方法
{
公共静态int递归二进制搜索(int[]数组,int l,int h,int x)
{
l=0;
h=数组长度-1;
while(l数组[mid]
返回递归二进制搜索(数组,mid+1,h,x);
}
返回-1;
}
公共静态void main(字符串[]args)
{
int[]a=
{2, 8, 12, 14, 16, 19, 24, 28, 31, 33,    // 0 - 9
39, 40, 45, 49, 51, 53, 54, 56, 57, 60,  // 10-19
63, 69, 77, 82, 88, 89, 94, 96, 97};     // 20-28
for(int i=0;i
您总是设置
l
h
并忽略参数。因此,当您递归时,您将再次从正中间开始:

public static int RecursiveBinarySearch(int[] array, int l, int h, int x) {
  l = 0;
  h = array.length -1;

我不明白为什么要在递归函数中使用while循环

public class RecursiveMethod {
    public static int RecursiveBinarySearch(int[] array, int l, int h, int x) {
        if (h>=l)
            int mid = l + (h-l)/2;

        if (x == array[mid])
            return mid;
        else if ( l == h)
            return -1;
        else if (x < array[mid])
            return RecursiveBinarySearch(array, l, mid-1, x);
        if (x > array[mid])   // last possibility: x > array[mid]
            return RecursiveBinarySearch(array, mid+1, h, x);
    }
}
公共类递归方法{
公共静态int递归二进制搜索(int[]数组,int l,int h,int x){
如果(h>=l)
int mid=l+(h-l)/2;
if(x==数组[mid])
中途返回;
else如果(l==h)
返回-1;
else if(xarray[mid])//最后一种可能性:x>array[mid]
返回递归二进制搜索(数组,mid+1,h,x);
}
}
编辑
正如Brett Okken所指出的,你不应该在调用中覆盖你的l,h。

Err l和h是递归边界,他确实用其中一个调用它,并将中点设置为另一个:/@krism他可以用
l
h
的任何值调用方法,这并不重要。它们是immedia重新分配给
0
和值的数量。我同意,但同时,算法的其余部分不起作用,或者如果起作用,则效率低下。调用中没有使用while循环。这将引发堆栈溢出,并且在每次递归调用期间重置
l
h
。很抱歉,这也是真的l、 但事实上,他也不需要while循环。我修复了我的解决方案。即使使用这个新的解决方案(不给l和h赋值),我也得到了“堆栈溢出”错误。@Hengameh在递归
RecursiveBinarySearch
时设置了新的l和h值。不确定原因,但我的中间计算在从Sublime复制后的上一次编辑中消失了。无论如何,它被重新添加了。改进了格式,澄清了问题。