搜索算法始终返回-1

时间:2018-12-14 00:36:31

标签: java

我具有以下二进制和顺序搜索的实现。我对Java不太熟悉,在搜索元素时,这两种方法的实现总会返回-1:

import java.util.Random;
import java.util.Scanner;

public class searchArrayDemo {

    private static int binarySearch(int[] inputArr, int key) {
        int start = 0;
        int end = inputArr.length - 1;

        while (start <= end) {

            int middle = (start + end) / 2;

            if (key < inputArr[middle]) {
                end = middle - 1;
            }

            if (start > inputArr[middle]) {
                start = middle + 1;
            }

            if (start == inputArr[middle]) {
                return middle;
            }
        }
        return -1;
        }

    // This function returns index of element x in arr[]
    private static int search(int arr[],int x)
    {
        for (int i = 0; i < arr.length; i++) {
            // Return the index of the element if the element
            // is found
            if (arr[i] == x)
                return i;
        }

        // return -1 if the element is not found
        return -1;
    }


    // Driver method to test above
    public static void main(String args[])
    {
        int c, n, array[];
        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of elements: ");
        n = in.nextInt();
        array = new int[n];
        Random rand = new Random();
        for (c = 0; c < n; c++)
        {
            array[c] = rand.nextInt(100);
        }
        System.out.println("Elements: ");
        for (int i = 0; i < array.length; i++)
        {
            System.out.print(array[i] + " ");
        }

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the element to be searched: ");
        int k = sc.nextInt();

            System.out.println("\nSelect type of search\n");
            System.out.println("1. Binary search");
            System.out.println("2. Sequential search");

        int choice = in.nextInt();
        int binarySearchResult = binarySearch(array, choice);
        int sequentialSearchResult = search(array, choice);
        switch(choice) {
            case 1:
                if (binarySearchResult == -1) {
                    System.out.println("\n" + k + " element not found");
                }
                else {
                    System.out.println("\n"+ k +" element found at position "+ binarySearchResult);
                }
                break;
            case 2:
                if (sequentialSearchResult == -1) {

                    System.out.println(sequentialSearchResult + "");
                    System.out.println("\n" + k + " element not found");
                }
                else {
                    System.out.println("\n" + k + " element found at position " + sequentialSearchResult);
                }
                break;
        }
    }
}

我猜想这是一个愚蠢的错误,但是请您帮我。

2 个答案:

答案 0 :(得分:0)

是的。这个

int binarySearchResult = binarySearch(array, choice);
int sequentialSearchResult = search(array, choice);

应该是

int binarySearchResult = binarySearch(array, k);
int sequentialSearchResult = search(array, k);

因为k是您要搜索的元素,而choice应该是搜索的类型。还需要注意的是,在对binarySearch进行排序之前,您的array无法正常工作。

答案 1 :(得分:0)

我相信您希望在第一个函数的if语句中将“ start”替换为“ key”。另外(旁注),当开始+结束为奇数时,您的函数如何处理场景?这又是您希望函数如何处理该场景?

还要检查您的start = middle + 1;