为什么二元搜索的尝试会崩溃?

时间:2012-10-14 21:25:30

标签: java crash binary-search


我对二进制搜索的概念相当新,并且我正在尝试用Java编写一个用于个人练习的程序。我理解这个概念,但我的代码不起作用。

我的代码中发生了一个运行时异常导致Eclipse,然后我的计算机崩溃......但是这里没有编译时错误。

<小时/> 以下是我到目前为止:

public class BinarySearch
{
    // instance variables
    int[] arr;
    int iterations;

    // constructor
    public BinarySearch(int[] arr)
    {
        this.arr = arr;
        iterations = 0;
    }

    // instance method
    public int findTarget(int targ, int[] sorted)
    {
        int firstIndex = 1;
        int lastIndex = sorted.length;
        int middleIndex = (firstIndex + lastIndex) / 2;
        int result = sorted[middleIndex - 1];

        while(result != targ)
        {
            if(result > targ)
            {
                firstIndex = middleIndex + 1;
                middleIndex = (firstIndex + lastIndex) / 2;
                result = sorted[middleIndex - 1];

                iterations++;
            }
            else
            {
                lastIndex = middleIndex + 1;
                middleIndex = (firstIndex + lastIndex) / 2;
                result = sorted[middleIndex - 1];

                iterations++;
            }
        }
        return result;
    }


    // main method
    public static void main(String[] args)
    {
        int[] sortedArr = new int[]
        {
            1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29
        };
        BinarySearch obj = new BinarySearch(sortedArr);

        int target = sortedArr[8];
        int result = obj.findTarget(target, sortedArr);

        System.out.println("The original target was -- " + target + ".\n" +
                            "The result found was -- " + result + ".\n" +
                            "This took " + obj.iterations + " iterations to find.");        
    } // end of main method
} // end of class BinarySearch

4 个答案:

答案 0 :(得分:3)

int result = sorted[middleIndex - 1];

应该是

int result = sorted[middleIndex];

如果lastIndex = 1,您尝试访问sorted[-1]

并且

lastIndex = middleIndex + 1;

应该是

lastIndex = middleIndex - 1;

或者您可以尝试访问sorted的结尾。

并且,为了完整性而包括Ted Hopp spotted,您应该从

开始
firstIndex = 0;
lastIndex = sorted.length-1;

因为数组索引是从0开始的。

答案 1 :(得分:3)

在Java中,数组索引是从零开始的。也就是说,索引的有效范围是0到,但包括数组长度。您正在从1索引到数组长度。尝试替换它:

int firstIndex = 1;
int lastIndex = sorted.length;

使用:

int firstIndex = 0;
int lastIndex = sorted.length - 1;

此外,正如@Daniel's points out in his answer所示,在您更新lastIndex的情况下,更新应该是middleIndex - 1(而不是现在的middleIndex + 1)。

答案 2 :(得分:1)

方法findTarget()中的while循环无限运行。所以我猜你在运行时得到的错误应该与内存相关,因为它一直在运行。 你会考虑你的方法findTarget()的一些变化吗?如果是,请尝试下面的示例:

                 int firstIndex = 0;
                 int lastIndex = sorted.length-1;
                while (firstIndex <= lastIndex) {
                    middleIndex = (firstIndex + lastIndex) / 2;
                    if (sorted[middleIndex] == targ) {

                            return middleIndex;
                    } else if (sorted[middleIndex] < targ) {
                         iterations++;
                        firstIndex = middleIndex + 1;
                    } else {
                         iterations++;
                           lastIndex = middleIndex - 1;
                    }
            }
            return -1;

答案 3 :(得分:0)


我的索引逻辑不仅关闭了(正如Ted和Daniel上面所指出的那样),而且if语句中的代码块也应该与else的代码块一起切换。

这是更正后的代码:

public class BinarySearch
{
    // instance variables
    int[] arr;
    int iterations;

    // constructor
    public BinarySearch(int[] arr)
    {
        this.arr = arr;
        iterations = 0;
    }

    // instance method
    public int findTarget(int targ, int[] sorted)
    {
        int firstIndex = 0;
        int lastIndex = sorted.length - 1;
        int middleIndex = (firstIndex + lastIndex) / 2;
        int result = sorted[middleIndex];

        iterations++;

        while(result != targ)
        {
            if(result > targ)
            {
                lastIndex = middleIndex - 1;
                middleIndex = (firstIndex + lastIndex) / 2;
                result = sorted[middleIndex];

                iterations++;
            }
            else
            {
                firstIndex = middleIndex + 1;
                middleIndex = (firstIndex + lastIndex) / 2;
                result = sorted[middleIndex];

                iterations++;
            }
        }
        return result;
    }

    // main method
    public static void main(String[] args)
    {
        int[] sortedArr = new int[]
        {
            1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29
        };
        BinarySearch obj = new BinarySearch(sortedArr);

        int target = sortedArr[8];
        int result = obj.findTarget(target, sortedArr);

        System.out.println("The original target was -- " + target + ".\n" +
                "The result found was -- " + result + ".\n" +
                "This took " + obj.iterations + " iterations to find.");        
    } // end of main method
} // end of class BinarySearch