Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我将如何结合这两个项目?_Java_Arrays_Sorting - Fatal编程技术网

Java 我将如何结合这两个项目?

Java 我将如何结合这两个项目?,java,arrays,sorting,Java,Arrays,Sorting,我对java非常陌生,我在我的课程中遇到了一些问题 基本上,我必须编写“一个Java程序,使用二进制搜索方法从这个列表中查找值45.3={-3,10,5,24,45.3,10.5}” 我的代码如下: public class BinarySearch { public static final int NOT_FOUND = -1; public static int binarySearch(Integer[] a, int x) { int low=0

我对java非常陌生,我在我的课程中遇到了一些问题

基本上,我必须编写“一个Java程序,使用二进制搜索方法从这个列表中查找值45.3={-3,10,5,24,45.3,10.5}”

我的代码如下:

public class BinarySearch
{
    public static final int NOT_FOUND = -1;
    public static int binarySearch(Integer[] a, int x)
    {
        int low=0;
        int high = a.length - 1;
        int mid;
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (a[mid].compareTo(x)<0)
                low = mid + 1;
            else if (a[mid].compareTo(x) > 0)
                high = mid - 1;
            else
                return mid;
        }
            return NOT_FOUND;
    }

    public static void main(String[] args)
    {
        int x = (453/10);
        int y = (105/10);
        int SIZE = 6;
        Integer [] a = {-3, 10, 5, 24, x, y};
        System.out.println("45.3 found at " +binarySearch(a, x));
    }
}
公共类二进制搜索
{
公共静态final int NOT_FOUND=-1;
公共静态int二进制搜索(整数[]a,int x)
{
int低=0;
int高=a.长度-1;
int mid;

而(低只需复制粘贴程序进行合并即可。幸运的是没有任何变量冲突

public class BubbleSortAndBinarySearch
{
    public static final int NOT_FOUND = -1;
    public static int binarySearch(Integer[] a, int x)
    {
        int low=0;
        int high = a.length - 1;
        int mid;
        while (low <= high)
        {
            mid = (low + high) / 2;
            if (a[mid].compareTo(x)<0)
                low = mid + 1;
            else if (a[mid].compareTo(x) > 0)
                high = mid - 1;
            else
                return mid;
        }
            return NOT_FOUND;
    }

    public static void main(String[] args)
    {
        int x = (453/10);
        int y = (105/10);
        int SIZE = 6;
        Integer [] a = {-3, 10, 5, 24, x, y};

        int b = a.length;
        int c, d, e;
        System.out.print("Original Order : ");
        for (c = 0; c < b; c++)
        {
            System.out.print(" " + a[c]);
        }

        System.out.println("\n");
        System.out.print("Ascending Order : ");
        for (d=1; d < b; d++)
        {
            for (c=0; c < b-d; c++)
             {
                if (a[c] > a[c+1])
                {
                    int f = a[c];
                    a[c] = a[c+1];
                    a[c+1] = f;
                }
            }
        }
        for(c = 0; c < b; c++)
        {
            System.out.print(" " + a[c]);
        }

        System.out.println(); // inserting this will make the result better to read

        System.out.println("45.3 found at " +binarySearch(a, x));
    }
}
公共类BubbleSortAndBinarySearch
{
公共静态final int NOT_FOUND=-1;
公共静态int二进制搜索(整数[]a,int x)
{
int低=0;
int高=a.长度-1;
int mid;
而(低a[c+1])
{
int f=a[c];
a[c]=a[c+1];
a[c+1]=f;
}
}
}
对于(c=0;c
然后,修改程序以正确处理非整数值,如45.3和10.5。目前,此代码仅适用于整数,除字符串中的外,未使用值45.3。

public class Search{
public class Search {
    public final static int NOT_FOUND = -1;

    public static double[] bubbleSort(double[] a) {
        int length = a.length;
        System.out.print("Original Order : ");
        for (int i = 0; i < length; i++) {
            System.out.print(" " + a[i]);
        }
        System.out.println("\n");
        System.out.print("Ascending Order : ");
        for (int i = 1; i < length; i++) {
            for (int j = 0; j < length - j; j++) {
                if (a[j] > a[j + 1]) {
                    double f = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = f;
                }
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.print(" " + a[i]);
        }
        System.out.println();
        return a;
    }

    public static int binarySearch(double[] a, double x) {
        int low = 0;
        int high = a.length - 1;
        int mid;
        while (low <= high) {
            mid = (low + high) / 2;
            if (a[mid] - x < 0)
                low = mid + 1;
            else if (a[mid] - x > 0)
                high = mid - 1;
            else {
                return mid;
            }
        }
        return NOT_FOUND;
    }

    public static void main(String[] args) {
        double[] array = { -3, 10, 5.0, 24, 45.3, 10.5 };
        double[] sortedArray = bubbleSort(array);
        System.out.println(binarySearch(sortedArray, 45.3));
    }
}
未找到公共最终静态int=-1; 公共静态双[]泡泡端口(双[]a){ int length=a.length; 系统输出打印(“原始订单:”); for(int i=0;ia[j+1]){ 双f=a[j]; a[j]=a[j+1]; a[j+1]=f; } } } for(int i=0;i
Integer
无法存储
45.3
10.5
。使用
BigDecimal
更好,因为它不会丢失精度。
public class Search {
    public final static int NOT_FOUND = -1;

    public static double[] bubbleSort(double[] a) {
        int length = a.length;
        System.out.print("Original Order : ");
        for (int i = 0; i < length; i++) {
            System.out.print(" " + a[i]);
        }
        System.out.println("\n");
        System.out.print("Ascending Order : ");
        for (int i = 1; i < length; i++) {
            for (int j = 0; j < length - j; j++) {
                if (a[j] > a[j + 1]) {
                    double f = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = f;
                }
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.print(" " + a[i]);
        }
        System.out.println();
        return a;
    }

    public static int binarySearch(double[] a, double x) {
        int low = 0;
        int high = a.length - 1;
        int mid;
        while (low <= high) {
            mid = (low + high) / 2;
            if (a[mid] - x < 0)
                low = mid + 1;
            else if (a[mid] - x > 0)
                high = mid - 1;
            else {
                return mid;
            }
        }
        return NOT_FOUND;
    }

    public static void main(String[] args) {
        double[] array = { -3, 10, 5.0, 24, 45.3, 10.5 };
        double[] sortedArray = bubbleSort(array);
        System.out.println(binarySearch(sortedArray, 45.3));
    }
}