原始类型的二分搜索

Binary Search with Primitive Types

我目前正在 class 进行 Java 编程,并且对 Java 完全陌生。我正在尝试创建一个程序,它将使用二进制搜索值 45.3

class findValue {
public static void main(String args[]) {
    double a[] = new double[6]; //declaration

    a[0] = -3; //initialization
    a[1] = 10;
    a[2] = 5;
    a[3] = 24;
    a[4] = 45.3;
    a[5] = 10.5;

    int n = a.length; //storing length of array
    int temp = 0; //declaring temporary storage place

    for (int i = 0; i < n; i++) {
        for (int j = 1; j < (n - i); j++) {

            if (a[j - 1] > a[j]) {
                temp = (int)a[j - 1];
                a[j - 1] = a[j];
                a[j] = temp; //bubble sorting
            };
        };
    };
    System.out.println("45.3 found" + binarySearch(a, 45.3));
};
public static void binarySearch(Integer[] a, int x) {
    int low = 0;
    int high = a.length - 1;
    int mid; //values for binary search

    while (low <= high) {
        mid = (low + high) / 2; //setting value for searching

        if (a[mid].compareTo(x) < 0) {
            low = mid + 1;
        }
        else if (a[mid].compareTo(x) > 0) {
            high = mid - 1;
        };
    };
};

这是我得到的编译器错误:

Line: 25
method binarySearch in class findValue cannot be applied to given types;
required: java.lang.Integer[],int
found: double[],double
reason: actual argument double[] cannot be converted to java.lang.Integer[] by method invocation conversion

来自您对方法的声明public static void binarySearch(Integer[] a, int x) { binarySearch 需要一个 Integer 数组和一个 int 作为参数, 当您在 line 25 中调用 binary search 时使用双精度数组和双精度作为参数,因此异常。

您不能将 double 转换为 int,因为 double 的 "information" 比 int 多。 double 43.5 转换为 int 会丢失 .5

(我知道还有很大的改进空间,但我只是建议程序运行所需的最少更改数量)

方法

public static void binarySearch(Integer[] a, int x) {...}

需要整数,但我们希望它使用双精度数。这意味着参数应该是一个双精度数组,并且要查找的双精度数:

public static void binarySearch(double[] a, double x) {...}

这就是说,我们知道这个函数会return一个int,所以我们设置return类型:

public static double binarySearch(double[] a, double x) {...}

现在,最后,我们必须 return 通过在方法末尾添加以下内容(稍后)来查找我们要查找的数字:

return mid;

最后的结果应该是:

class findValue {
    public static void main(String args[]) {
        double a[] = new double[6]; //declaration

        a[0] = -3; //initialization
        a[1] = 10;
        a[2] = 5;
        a[3] = 24;
        a[4] = 45.3;
        a[5] = 10.5;

        int n = a.length; //storing length of array
        int temp = 0; //declaring temporary storage place

        for (int i = 0; i < n; i++) {
            for (int j = 1; j < (n - i); j++) {

                if (a[j - 1] > a[j]) {
                    temp = (int)a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp; //bubble sorting
                }
            }
        }
        System.out.println("45.3 found: " + binarySearch(a, 45.3));
    }
    public static int binarySearch(double[] a, double x) {
        int low = 0;
        int high = a.length - 1;
        int mid = (low + high) / 2; //values for binary search

        while (low <= high) {
            mid = (low + high) / 2; //setting value for searching

            if (Double.compare(a[mid], (double)x) < 0) {
                low = mid + 1;
            }
            else if (Double.compare(a[mid], (double)x) > 0) {
                high = mid - 1;
            }
        }
        return mid;
    }
}

输出:

45.3 found: 5