自定义二进制搜索中的ArrayIndesOutOfBoundsException?

时间:2012-03-11 07:01:01

标签: java arrays algorithm binary-search

我正在尝试用Java实现二进制搜索:

import java.util.Scanner;

public class Search 
{
public static void sequential_search(int[] array,int search_variable)
{
    boolean flag = false;
    for(int loop=0;loop<array.length;loop++)
    {
        if(array[loop] == search_variable)
        {
            flag = true;
            break;
        }
    }
    if(flag == true)
        System.out.println("The value is present");
    else
        System.out.println("The value is absent");
}

public static int binary_search_recursive(int[] array,int low,int high,int search_variable)
{
    if(low > high)
    {
        return 0;
    }
    else
    {
        int mid;
        mid = (low+high)/2;
        if(array[mid] == search_variable)
        {
            return 1;
        }
        else if (array[mid] > search_variable)
        {
            return binary_search_recursive(array, low, mid - 1, search_variable);
        }
        else 
        {
            return binary_search_recursive(array, mid + 1, high, search_variable);
        }
    }
}

public static void main(String[] args) 
{
    int[] array = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
    int search_variable;

    System.out.println("Please enter the number to be search:");
    Scanner number = new Scanner(System.in);
    search_variable = number.nextInt();
    System.out.println("The number to be searched is "+search_variable);

    //sequential_search(array,search_variable);

    if ((binary_search_recursive(array, 0, array.length, search_variable)) == 0)
        System.out.println("The value is present");
    else
        System.out.println("The value is absent");

}

}

当我输入ArrayOutOfBoundsException时,当我输入算法找不到的术语时,我只是感到困惑。

Please enter the number to be search:
12
The number to be searched is 12
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Search.binary_search_recursive(Search.java:32)
    at Search.binary_search_recursive(Search.java:42)
    at Search.binary_search_recursive(Search.java:42)
    at Search.binary_search_recursive(Search.java:42)
    at Search.main(Search.java:59)

对此的任何输入都会有所帮助。 谢谢:))

2 个答案:

答案 0 :(得分:4)

我认为这里的问题是,您的lowhigh变量代表范围[低,高],排除high值。这意味着你的基本情况,目前是

if(low > high)
{
    return 0;
}

应该是

if(low >= high)
{
    return 0;
}

因为如果你有[低,低]范围,则​​没有任何元素。在您终止前要求low > high意味着您需要低到实际超过high,这是不可能的。

如果我对此是正确的,这也意味着你对该函数的一个递归调用具有错误的上限。具体来说,这段代码:

    else if (array[mid] > search_variable)
    {
        return binary_search_recursive(array, low, mid - 1, search_variable);
    }

应该是

    else if (array[mid] > search_variable)
    {
        return binary_search_recursive(array, low, mid, search_variable);
    }

因为如果high是独占的,那么传递mid作为上限是获取所有内容但不包括中间值的正确方法。

希望这有帮助!

答案 1 :(得分:0)

if ((binary_search_recursive(array, 0, array.length, search_variable)) == 0)

如果使用这些参数,最终会检查数组[array.length]。 10个元素表示array.length是10,但第10个索引当然是未定义的。

if ((binary_search_recursive(array, 0, array.length-1, search_variable)) == 0)
相关问题