我如何将这两个程序结合起来?

时间:2016-05-12 00:17:44

标签: 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;
        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));
    }
}

但是我意识到它没有排序所以我使用了一个我已经拥有的简单BubbleSort并插入了数字:

class BubbleSort
{
public static void main(String args[])
{

    int x = (453/10);
    int y = (105/10);
    int 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]);
    }
}
}

但是在我上课时我不知道如何让类文件在某种程度上协同工作,或者将它们全部放在一个.java或.class文件中。

任何提示?

谢谢!

2 个答案:

答案 0 :(得分:0)

从复制&amp;开始粘贴程序进行合并。幸运的是,没有任何变量碰撞。

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));
    }
}

然后,修改程序以正确处理非整数值,如45.3和10.5。目前,此代码仅适用于整数,除了字符串外,不使用值45.3。

答案 1 :(得分:0)

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));
    }
}