【问题标题】:Binary and linear search program to find an integer. Responds yes if it is there and no if it is not. How to get the binary search working?寻找整数的二进制和线性搜索程序。如果存在则回答是,如果不存在则回答否。如何让二进制搜索工作?
【发布时间】:2014-02-22 04:41:38
【问题描述】:

我正在尝试使二进制搜索正常工作。它应该询问您数组的大小,要输入数组的整数并搜索数组以找到您正在寻找的数字以及找到它需要多少次探测或以未找到的数字进行响应。我正在遵循该算法,但它要么以无限循环响应,要么根本不响应,因为它永远运行。有什么建议吗?

package assignment3;

import java.util.Scanner;

public class search{

    public static void main(String[] arg){

        Scanner range=new Scanner(System.in);

        System.out.println("Pick your array size.");

        int element=range.nextInt();

        int[] array=new int[element];

        Scanner array_list=new Scanner(System.in);

        if(element<=0){

            System.out.println("The array size you chose is not supported. You must chose again");

            Scanner tryagain=new Scanner(System.in);

            System.out.println("Try again");

            element=tryagain.nextInt();    
        }

        System.out.println("Now enter all the numbers in your array");

        String list=array_list.nextLine();

        String[] newlist=list.split("\\ ");

        int lengthofarray=newlist.length;

        for(int i=0; i<lengthofarray; i++){

            array[i]=Integer.parseInt(newlist[i]);

            System.out.println(i+" || "+array[i]);

        }

        Scanner linearSearch=new Scanner(System.in);

        System.out.println("Pick a number to see if it is in the array and how many times it took to find it.");

        int linear=linearSearch.nextInt();
        Scanner linear2=new Scanner(System.in);
        System.out.println("Now we will try the same thing with a binary search. Pick a number");
        int binarysearch=linear2.nextInt();
        int low=0;
        int high=lengthofarray-1;
        int middle;

        while(low<=high){

            middle=(low+high)/2;

            if(array[middle]>binarysearch){
                high=middle-1;
            }

            else if(array[middle]<binarysearch){
                low=middle+1;
            }
        }
        if(array[low]==binarysearch){
            System.out.println("Your number is in the array.");

        }
        else{
            System.out.println("your number is not in the array.");
        }
    }
}

【问题讨论】:

  • 除了缺少array[middle] == binarysearch 的情况外,二分查找仅适用于已排序的数组。您需要先致电Arrays.sort(array)

标签: java arrays search binary


【解决方案1】:

我认为,问题在于您错过了array[middle] == binarysearch 时的情况。看看你的二分搜索的逻辑,你会发现lowhigh 都没有改变,循环永远不会终止。

试试这个:

while (low <= high) {
    middle = (low + high) / 2;
    if (array[middle] > binarysearch) {
        high = middle - 1;
    } else if (array[middle] == binarysearch) {
        low = middle;
        break;
    } else {
        low = middle + 1;
    }
}

【讨论】:

  • 这看起来绝对是对现有代码的改进。
【解决方案2】:
/*PROGRAM FOR BINARY SEARCH*/
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[100];
int a,n,i,item,loc,temp;
clrscr();
n=1;
loc=0;
printf("\n\Enter total no of data: ");
scanf("%d",&a);
for(i=0;i<a;i++)
{
printf("\n Enter a[%d]:",i);
scanf("%d",&arr[i]);
loc=loc+1;
}
printf("\nEnter ITEM u want to search:");
scanf("%d",&item);
temp=loc+1;
arr[temp]=item;
while(n<=a && arr[n]!=item)
{
n=n+1;
}
if(n==arr[temp])
{
n=0;
printf("\nn=%d",n);
}
else
{
printf("\n ITEM is present AT a[%d]",n);
}

getch();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 2012-03-30
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多