gpt4 book ai didi

java - 如何打印计算机为二进制搜索所做的数学运算

转载 作者:搜寻专家 更新时间:2023-11-01 03:20:35 25 4
gpt4 key购买 nike

我正在开发一个程序,它会要求用户输入一个介于 3000-3100 之间的值,然后进行二进制搜索并显示它进行了多少次比较。总体而言,程序的搜索部分运行良好;但是,我的教授要我打印进行数学计算的程序。例如,我需要程序显示计算机进行二进制搜索数学运算,并显示程序查找输入数字所需的比较。

我有一个 comparisonCount,当我进行比较时我会递增它,但结果不是我认为应该的结果。例如,我的教授说如果你输入 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);
}
}
}

最佳答案

七次迭代是在一组一百个数字中找到一个数字所需的最大值。那是因为log<sub>2</sub>100介于 6 和 7 之间,您需要使用这两个值中的较高值才能确定找到它。

但是,由于其工作方式,某些特定 值可能会更少。让我们更改您的代码,使其输出它在每个阶段所做的事情(只是添加了一些输出,标有 //<<here//<< 的部分):

while ((array[position] != key) && (lowerbound <= upperbound)) {
System.out.print(String.format( //<<here
"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( //<<here
"too high, hi:=%2d", upperbound)); //<<
} else {
lowerbound = position + 1;
System.out.println(String.format( //<<here
"too low, lo:=%2d", lowerbound)); //<<
}
position = (lowerbound + upperbound) / 2;
}
if (lowerbound <= upperbound) {
System.out.println(String.format( //<<here
"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!

关于java - 如何打印计算机为二进制搜索所做的数学运算,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31572651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com