【问题标题】:Errors in Code (Need Help)代码错误(需要帮助)
【发布时间】:2017-12-04 23:28:28
【问题描述】:

我正在尝试使此代码正常工作,因为它对于我目前正在从事的项目至关重要。

import java.util.Scanner;

public class BinarySearch
{
    int binarySearch(int arr[], int l, int r, int x)
    {
        if (r>=l)
    {
        int mid = l + (r - l)/2;

        if (arr[mid] == x)
           return mid;

        if (arr[mid] > x)
           return binarySearch(arr, l, mid-1, x);

        return binarySearch(arr, mid+1, r, x);
    }
    return -1;
}

public static void main(String args[])
{
    BinarySearch ob = new BinarySearch();
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter number of inputs:");
        int i = sc.nextInt();
        int arr[] = new int[i];
    System.out.println("Enter array of inputs:");
        for(int j = 0;j < i; j++){
            arr[j] = sc.nextInt();
    }
    System.out.println("What number do you want the index from");
        int n = arr.length;
        int x = sc.nextInt();
        int result = ob.binarySearch(arr,0,n-1,x);
        if (result == -1)
            System.out.println("FAILURE");
        else
            System.out.println("Element found at index "+result + ".");
    }
}

我希望结果是正常输入,数组可以在其中运行。 我得到的实际结果是“可能有无限循环”的超时错误。

【问题讨论】:

  • 如果您在自己实现二进制搜索时遇到问题,只是一个快速提示,Arrays 类中有重载的静态方法可以执行二进制搜索,请看这里:docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
  • 打印出你的数组以及 main() 中 x 的值。
  • 您的binary_search 方法存在问题。它总是返回正确的结果,3 和 -1。想想看。运行时我没有出现“无限循环”错误。

标签: java arrays int binary-search


【解决方案1】:

binarySearch 对我来说似乎很好。

但是二分查找仅适用于排序(通过比较)排序的数组。

超时确实意味着循环或递归不会结束。 考虑

  • 递归案例:l
  • 给定int mid = l + (r - l)/2
  • 因此 l
  • 然后binarySearch(arr, l, mid-1, x); 作用于[l, &lt;r](更小)
  • 然后binarySearch(arr, mid+1, r, x); 作用于[&gt;l, r](更小)

递归将终止。

即使在无序数组上(产生垃圾)。

看起来更像是扫描仪的使用存在问题。我没有看到 hasNextLinehasNextIntnextIntnextLine 的干净用法。

Arrays 类可以提供一个sort(也有自己的binarySearch)。


代码

没有测试,一般我不会用ScannerSystem.in, 所以大概可以写得更简洁:

BinarySearch ob = new BinarySearch();
Scanner sc = new Scanner(System.in);

System.out.println("Enter number of inputs:");
if (sc.hasNextLine() && sc.hasNextInt()) {
    int i = sc.nextInt();
    sc.nextLine();

    int arr[] = new int[i];
    int n = arr.length;

    System.out.println("Enter array of inputs:");
    for (int j = 0; j < i; j++) {
        if (sc.hasNextLine() && sc.hasNextInt()) {
            arr[j] = sc.nextInt();
            sc.nextLine();
        } else {
            ...
            System.exit(1);
        }
    }

    // Important, binary search relies on the array being sorted:
    Arrays.sort(arr);
    System.out.printf("The sorted array is %s%n", Arrays.toString(arr));

    System.out.println("What number do you want the index from?");
    if (sc.hasNextLine() && sc.hasNextInt()) {
        int x = sc.nextInt();
        sc.nextLine();
        int result = ob.binarySearch(arr, 0, n-1, x);

【讨论】:

  • 感谢您的帮助,但您能指出在哪里以及如何修复上述代码吗?我在这方面有点麻烦。
  • 给出更正的代码。我没有使用 IDE,所以我希望它或多或少按照我的想法行事。
猜你喜欢
  • 1970-01-01
  • 2011-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2011-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多