【问题标题】:Bug in beginner Binary search初学者二进制搜索中的错误
【发布时间】:2019-06-19 22:14:32
【问题描述】:

当数组中存在搜索值(SV)时显示输出。但如果它不存在则不会。它不断要求输入并不断循环。

这是我第一次尝试为二进制搜索编写代码

import java.util.Scanner;

public class Binary_search {

    public static void main() {
        Scanner input = new Scanner(System.in);
        int start = 0;
        int end = arr.length;

        //I used flag to end the loop 
        int Flag = 0;
        int mid = 0;
        int SV = input.nextInt();

        //here I enter values in the array
        for (int x = 0; x <= 4; x++) {
            arr[x] = input.nextInt();
        }

        //here I start a loop for the search 
        while (Flag == 0) {
            mid = (start + end) / 2;

            if (mid == SV) {
                System.out.println("Number Found" + arr[mid]);
                Flag = Flag + 1;
            } else if (mid > SV) {
                end = mid - 1;
            } else if (mid < SV) {
                start = mid + 1;
            }

            //this was the second possibility if the number was not present   
            else if (start == end) {
                Flag = Flag + 1;
                System.out.println("Number not found");
            }
        }
    }
}

如果 SV 存在于数组中,它将显示它的位置,“找到的数字”+ arr[mid]。但如果它不存在,它应该输出“找不到号码”,但是,这不会发生,它会一直要求输入。

【问题讨论】:

  • 你用的是什么输入法?请阅读minimal reproducible example 并相应地增强您的问题。您的代码也缺少设置该数组的方式。请注意,Java 中的变量名采用驼峰式,类名采用大写,其中没有 _!
  • 我提醒你,为了使二分搜索起作用,被搜索的数组必须是排序的。

标签: java arrays binary-search bluej


【解决方案1】:

您可以像这样创建简单的方法。 请比较并检查您需要更正的地方。

public class Test {

    public static void main(String[] args) {
        int arr[] = { 10, 20, 30, 40, 50 };
        int key = 3;
        binarySearchExample(arr, key);
    }

    public static void binarySearchExample(int arr[], int key) {
        int first = 0;
        int last = arr.length - 1;
        int mid = (first + last) / 2;
        while (first <= last) {
            if (arr[mid] < key) {
                first = mid + 1;
            } else if (arr[mid] == key) {
                System.out.println("Element is found at index: " + mid);
                break;
            } else {
                last = mid - 1;
            }
            mid = (first + last) / 2;
        }
        if (first > last) {
            System.out.println("Element is not found!");
        }
    }

}

【讨论】:

  • 非常感谢。我将使用此代码。当java提供它时,我试图不使用这个功能,我只是一个初学者:p。再次感谢您的帮助。
【解决方案2】:

您可以创建一个这样的类来输入用户的数字并将其存储在一个数组中,然后使用 binary 在数组中搜索另一个输入的数字搜索并显示其位置。

import java.util.*;
class BinarySearch
{
   public static void main()
   {
      int i, num, item, array[], first, last, middle;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter number of elements:");
      num = sc.nextInt();
      //array gets created by inputted space
      array = new int[num];
      System.out.println("Enter " + num + " integers");
      for (i = 0; i < num; i++)
          array[i] = sc.nextInt();

      System.out.println("Enter the number to be searched:");
      item = sc.nextInt();
      first = 0;
      last = num - 1;
      middle = (first + last)/2;

      //search starts
      while( first <= last )
      {
         if ( array[middle] < item )
           first = middle + 1;
         else if ( array[middle] == item )
         {
           //number found in
           System.out.println(item + " is found at location " + (middle + 1));
           break;
         }
         else
         {
             last = middle - 1;
         }
         middle = (first + last)/2;
      }
      if ( first > last )
          //number not found in array
          System.out.println(item + " is not found.\n");
   }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多