Java 为什么这种二进制搜索的尝试会失败?
Java 为什么这种二进制搜索的尝试会失败?,java,crash,binary-search,Java,Crash,Binary Search,我对二进制搜索的概念还比较陌生,我正试图编写一个程序,用Java实现这一点,以供个人练习。我很理解这个概念,但我的代码不起作用。 我的代码中发生了一个运行时异常,导致Eclipse崩溃,然后我的计算机崩溃。。。不过这里没有编译时错误。 以下是我到目前为止的情况: public class BinarySearch { // instance variables int[] arr; int iterations; // constructor pub
我对二进制搜索的概念还比较陌生,我正试图编写一个程序,用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
应该是
int result = sorted[middleIndex];
lastIndex = middleIndex - 1;
如果lastIndex=1
,则尝试访问排序的[-1]
及
应该是
int result = sorted[middleIndex];
lastIndex = middleIndex - 1;
或者,您可以尝试访问排序后的末尾
并且,为了完整性,您应该从
firstIndex = 0;
lastIndex = sorted.length-1;
由于数组索引是基于0的。在Java中,数组索引是基于零的。也就是说,索引的有效范围为0,最大为数组长度,但不包括数组长度。您正在从1索引到数组长度。尝试替换此:
int firstIndex = 1;
int lastIndex = sorted.length;
与:
同样,在更新lastIndex
时,更新应该是middleIndex-1
(而不是现在的middleIndex+1
。方法findTarget()中的while循环无限运行。所以我猜测,您在运行时遇到的错误应该与内存有关,因为它会一直运行。
您会考虑方法FAIN TAG()中的一些更改吗?如果是,请尝试以下示例:
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;
int firstIndex=0;
int lastIndex=sorted.length-1;
而(第一指数
不仅我的索引逻辑关闭了(正如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
错误的stacktrace是什么?定义“崩溃”和“运行时异常”。这不是一个猜测游戏。很抱歉让你一直猜测。问题是这是一个无限循环,如果你愿意的话,这是一个stackoverflow错误(在多个方面?)。好的地方,我甚至没有看到!@DanielFischer-谢谢。关于更新lastIndex
,你的理解也很好。我错过了。我们可以一起解决他的问题。:)谢谢@TedHopp的帮助!我希望我能同时选择你和丹尼尔的答案作为最好的答案,哈哈。是的,我对那里的索引越来越纠结,不确定我在想什么(我应该更清楚)。另外,我的逻辑也关闭了——if语句中的代码块应该与else语句中的代码块切换。我已经在下面发布了我的更正代码作为答案。再次感谢!谢谢@Daniel!你在这里的具体建议非常有用。我的if-else逻辑也是反向的——将if语句中的代码块与else语句中的代码块切换成功。有了这个修正,并有了你的建议,我已经在下面发布了我的修正代码作为答案。谢谢@吉米,正好是关于无限循环的,不知道为什么我以前没有看到。while和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