错误的二分搜索输出

huangapple go评论71阅读模式
英文:

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(&quot;Enter a value: &quot;);
searchValue = scanner.nextInt();
foundValue = searchMethod(numbers, searchValue);
if (value == -1) {
System.out.println(&quot;Didn&#39;t find the value.&quot;);
} else {
System.out.println(&quot;found the value.&quot;);
}
}
// Here&#39;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 &amp;&amp; first &lt; = last) {
middle = (first + last) / 2;
if (array[middle] == value) {
found = true;
position = middle;
} else if (array[middle] &gt; 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;       
    }
}

请注意,我已经按照您的要求提供了翻译后的代码部分,但您提供的代码中存在一些不一致之处,例如方法名和变量名不匹配,变量 valuesearchValue 之间的混淆等。如果您需要完全一致的代码,请您核对一下原始代码并进行必要的更改。

英文:

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(&quot;Enter a value: &quot;);
searchValue = scanner.nextInt();
// Sort in ascending order
Arrays.sort(numbers);
foundValue = searchMethod(numbers,searchValue );
if(value == -1){
System.out.println(&quot;Didn&#39;t find the value.&quot;);
}else{
System.out.println(&quot;found the value.&quot;);
}
}
// Here&#39;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 &amp;&amp; first &lt;= last){
middle = (first + last) / 2;
if(array[middle] == value){
found = true;
position = middle;
}
else if(array[middle] &gt; 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 &lt; array.length; i++) {
for (int j = i; j &gt; 0; j--) {
if (array[j] &lt; 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 错误的二分搜索输出

huangapple
  • 本文由 发表于 2020年9月15日 12:28:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/63895084.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定