英文:
Incorrect binary search output
问题
我在数组的二分查找中遇到了问题。虽然程序能运行,但每次输出的结果都是错误的。我不太确定问题出在哪里。如果有人能帮忙,将不胜感激。
public class chapter7Assignment {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = {1, 566, 18, 1, 8, 5, 18, 4, 3, 8};
int searchValue;
int foundValue;
System.out.print("Enter a value: ");
searchValue = scanner.nextInt();
foundValue = searchMethod(numbers, searchValue);
if (value == -1) {
System.out.println("Didn't find the value.");
} else {
System.out.println("Found the value.");
}
}
// 下面是用于传递数组和扫描器输入的搜索方法
public static int thisMethod(int[] array, int value) {
int middle;
int first = 0;
int last = array.length - 1;
int position = -1;
boolean found = false;
while (!found && first <= last) {
middle = (first + last) / 2;
if (array[middle] == value) {
found = true;
position = middle;
} else if (array[middle] > value) {
last = middle - 1;
} else {
first = middle + 1;
}
}
return position;
}
}
英文:
I'm having trouble with this binary search on an array. It runs but I get the incorrect output each time. I'm not too sure why. If anybody can help it would be greatly appreciated.
public class chapter7Assignment {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] numbers = {1, 566, 18, 1, 8, 5, 18, 4, 3, 8};
int searchValue;
int foundValue;
System.out.print("Enter a value: ");
searchValue = scanner.nextInt();
foundValue = searchMethod(numbers, searchValue);
if (value == -1) {
System.out.println("Didn't find the value.");
} else {
System.out.println("found the value.");
}
}
// Here's the search method used to pass the array and the scanner
// input into it
public static int thisMethod(int[] array, int value) {
int middle;
int first = 0;
int last = array.length - 1;
int position = -1;
boolean found = false;
while (!found && first < = last) {
middle = (first + last) / 2;
if (array[middle] == value) {
found = true;
position = middle;
} else if (array[middle] > value) {
last = middle - 1;
} else {
first = middle + 1;
}
}
return position;
}
}
答案1
得分: 0
如前所述,您需要首先对其进行排序。此代码应该可行,在顶部不要忘记导入语句。
import java.util.Arrays;
public class chapter7Assignment{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] numbers = {1,566,18,1,8,5,18,4,3,8};
int searchValue;
int foundValue;
System.out.print("Enter a value: ");
searchValue = scanner.nextInt();
// Sort in ascending order
Arrays.sort(numbers);
foundValue = searchMethod(numbers,searchValue );
if(foundValue == -1){
System.out.println("Didn't find the value.");
}else{
System.out.println("Found the value.");
}
}
// Here's the search method used to pass the array and the scanner
// input into it
public static int searchMethod (int [] array, int value){
int middle;
int first= 0;
int last = array.length -1;
int position = -1;
boolean found = false;
while(!found && first <= last){
middle = (first + last) / 2;
if(array[middle] == value){
found = true;
position = middle;
}
else if(array[middle] > value){
last = middle -1;
}else{
first = middle + 1;
}
}
return position;
}
}
请注意,我已经按照您的要求提供了翻译后的代码部分,但您提供的代码中存在一些不一致之处,例如方法名和变量名不匹配,变量 value
和 searchValue
之间的混淆等。如果您需要完全一致的代码,请您核对一下原始代码并进行必要的更改。
英文:
As people mentioned, you need to sort it first. This code should work, don't forget the import statement at the top.
import java.util.Arrays;
public class chapter7Assignment{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] numbers = {1,566,18,1,8,5,18,4,3,8};
int searchValue;
int foundValue;
System.out.print("Enter a value: ");
searchValue = scanner.nextInt();
// Sort in ascending order
Arrays.sort(numbers);
foundValue = searchMethod(numbers,searchValue );
if(value == -1){
System.out.println("Didn't find the value.");
}else{
System.out.println("found the value.");
}
}
// Here's the search method used to pass the array and the scanner
// input into it
public static int thisMethod (int [] array, int value){
int middle;
int first= 0;
int last = array.length -1;
int position = -1;
boolean found = false;
while(!found && first <= last){
middle = (first + last) / 2;
if(array[middle] == value){
found = true;
position = middle;
}
else if(array[middle] > value){
last = middle -1;
}else{
first = middle + 1;
}
}
return position;
}
}
}
答案2
得分: 0
正如我们所知,对于二分搜索的实现,我们需要一个已排序的数组。因此,您首先需要使用以下代码片段对数组进行排序:
private static void sortArray(int[] array) {
int temp;
for (int i = 1; i < array.length; i++) {
for (int j = i; j > 0; j--) {
if (array[j] < array[j - 1]) {
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
}
在调用二分搜索方法之前,请调用此方法,如下所示:
sortArray(numbers);
foundValue = searchMethod(numbers, searchValue);
这样您将获得确切的答案。
祝您编码愉快!
英文:
As we know, for binary search implementation we need a sorted array. So you need to sort your array first by using following code snippet
private static void sortArray(int[] array){
int temp;
for (int i = 1; i < array.length; i++) {
for (int j = i; j > 0; j--) {
if (array[j] < array [j - 1]) {
temp = array[j];
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
Call this method before calling binary search method as below
sortArray(numbers);
foundValue = searchMethod(numbers,searchValue );
You will have the exact answer.
Happy Coding
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论