我正在开发一个程序,要求用户输入3000-3100之间的值并进行二进制搜索并显示它进行了多少次比较.整体而言,该计划的搜索部分工作正常;但是,我的教授要我打印这个程序进行数学运算.例如,我需要程序显示计算机进行二进制搜索数学运算,并显示程序查找输入数字所需的比较.
我有一个比较计数器,当我进行比较时,我正在递增它,但结果不是我认为应该是的.例如,我的教授说如果你输入3067应该有7个比较,但目前程序说4,而计数器说3.
你能帮我找一下这个差异的原因吗?
这是代码:
package binary.search;
import java.util.Scanner;
public class BinarySearch
{
public static void binarySearch(int[] array, int lowerbound, int upperbound, int key)
{
int position;
int comparisonCount = 1; // counting the number of comparisons
// To start, find the subscript of the middle position.
position = (lowerbound + upperbound) / 2;
//System.out.println();
while ((array[position] != key) && (lowerbound <= upperbound))
{
comparisonCount++;
if (array[position] > key) // If the number is > key, ..
{
upperbound = position - 1; // decrease position by one.
}
else
{
lowerbound = position + 1; // Else, increase position by one.
}
position = (lowerbound + upperbound) / 2;
}
if (lowerbound <= upperbound)
{
System.out.println("The number " + key + " was found in array.");
System.out.println("The binary search found the number after " + comparisonCount + " comparisons.");
}
else
System.out.println("That number is not in this array. The binary search completed "
+ comparisonCount + " comparisons.");
}
public static void main(String[] args)
{
// Set up variables
int arrLength = 100;
Scanner inp = new Scanner(System.in);
int[] num = new int[arrLength]; //Create array
int repeat = 1; //Boolean for repeat loop
char yesNo;
int upperLim = 3100;
int lowerLim = 3000;
//Populate array
while (repeat == 1)
{
int value = 0;
int valid = 0;
for (int i = 0; i < num.length; i++)
{
num[i] = i + lowerLim;
}
//Get integer from user
do
{
System.out.print("Please enter a number between " + lowerLim + " and " + upperLim + ": ");
value = inp.nextInt();
if (value < lowerLim || value > upperLim)
{
System.out.print("That wasn't a valid number. Please try again. \n");
}
}
while (value < lowerLim || value > upperLim);
//Run binary search
binarySearch(num, 0, arrLength - 1, value);
do
{
valid = 0;
System.out.print("Would you like to rerun the program? Y for yes, N for no.\n");
yesNo = (inp.next()).charAt(0);
if (yesNo == 'Y')
{
repeat = 1;
valid = 1;
}
else if (yesNo == 'N')
{
repeat = 0;
valid = 1;
}
else
System.out.print("Not a valid response. \n");
}
while (valid != 1);
}
}
}
最佳答案 七次迭代是在一组中找到一个数字所需的最大值.那是因为log2100介于6到7之间,你需要使用这两个值中较高的值才能确定找到它.
但是,由于其工作方式,某些特定值可能较少.让我们更改你的代码,使它输出它在每个阶段所做的事情(只添加几个输出,标有//<< here to //<<
while ((array[position] != key) && (lowerbound <= upperbound)) {
System.out.print(String.format( //<
"Step %d, lo=%2d, hi=%2d, mid=%2d, [mid]=%4d, ",
comparisonCount, lowerbound, upperbound,
position, array[position])); //<<
comparisonCount++;
if (array[position] > key) {
upperbound = position - 1;
System.out.println(String.format( //<
"too high, hi:=%2d", upperbound)); //<<
} else {
lowerbound = position + 1;
System.out.println(String.format( //<
"too low, lo:=%2d", lowerbound)); //<<
}
position = (lowerbound + upperbound) / 2;
}
if (lowerbound <= upperbound) {
System.out.println(String.format( //<
"Step %d, lo=%2d, hi=%2d, mid=%2d, [mid]=%4d, found!",
comparisonCount, lowerbound, upperbound,
position, array[position])); //<<
System.out.println("The number " + key +
" was found in array.");
System.out.println("The binary search found the number after "
+ comparisonCount + " comparisons.");
}
例如,如果您正在搜索3049,您基本上会立即找到它:
Step 1: lo= 0, hi=99, mid=49, [mid]=3049, found!
对于您的特定值3067,它是这样的:
Step 1: lo= 0, hi=99, mid=49, [mid]=3049, too low, lo:=50
Step 2: lo=50, hi=99, mid=74, [mid]=3074, too high, hi:=73
Step 3: lo=50, hi=73, mid=61, [mid]=3061, too low, lo:=62
Step 4: lo=62, hi=73, mid=67, [mid]=3067, found!
因此,正如您所见,四次迭代.如果你想看到完整的七次迭代,你可以搜索3099:
Step 1, lo= 0, hi=99, mid=49, [mid]=3049, too low, lo:=50
Step 2, lo=50, hi=99, mid=74, [mid]=3074, too low, lo:=75
Step 3, lo=75, hi=99, mid=87, [mid]=3087, too low, lo:=88
Step 4, lo=88, hi=99, mid=93, [mid]=3093, too low, lo:=94
Step 5, lo=94, hi=99, mid=96, [mid]=3096, too low, lo:=97
Step 6, lo=97, hi=99, mid=98, [mid]=3098, too low, lo:=99
Step 7, lo=99, hi=99, mid=99, [mid]=3099, found!