初学者二进制搜索中的错误

时间:2019-01-26 06:05:40

标签: java arrays binary-search bluej

当数组中存在Search值(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]。但是,如果不存在,应该输出“找不到数字”,但是不会发生,并且会不断要求输入。

2 个答案:

答案 0 :(得分:0)

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

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!");
        }
    }

}

答案 1 :(得分:0)

您可以创建这样的类,以从用户输入数字并将其存储在数组中,并使用 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");
   }
}