使用递归方法进行冒泡排序,最后比较两个不同的数组

bubble sort with recursive method and at the end comper two different arrays

我正在编写一个代码,要求用户插入数组的数字,然后写入每个数字,在另一个数组中做同样的事情,最后比较第一个数组和第二个数组打印出所有数字的冒泡排序,因此对第一个和第二个数组一起进行冒泡排序。我在下面写了这个,但我不知道如何用一种方法比较两个不同的数组。

public static void main(String[] args) {

public static int[] macello(int[]A){

    for(int i=0; i<A.length-1; i++){

        for(int j=0; j<A.length-1-i;j++){
            if(A[j]>A[j+1]){
                int temp = A[j+1];
                A[j+1]= A[j];
                A[j] = temp;
            }
        }
    }
    return A;
}

public static void printArray2(int[]A){
    for(int i = 0; i<A.length; i++){
        System.out.print(A[i]+",");
    }
}

Scanner scan = new Scanner(System.in);

System.out.println("Insert the capacity's array1: ");

int n = scan.nextInt();

int[]numbers1 = {n};

    for(int i=0; i<n; i++){
        System.out.println("Insert the value of each numbers: ");
        int j =0;
        numbers1[j] = scan.nextInt();
        j++;
    }
    System.out.println("Insert the capacity's array2: ");
    int m = scan.nextInt();
    int[]numbers2 = {m};
    for(int i=0; i<m; i++){
        System.out.println("Insert the value of each numbers: ");
        int j=0;
        numbers2[j] = scan.nextInt();
        j++;
    }

    macello(Arrays.equals(numbers1,numbers2));
    printArray2(Arrays.equals(numbers1,numbers2));
}

}

您在评论中提到您已经解决了冒泡排序问题。所以我假设你有一个签名为 void bubbleSort(int[] arr).

的方法

您的代码表明您了解如何从用户那里获取数组,因此我们不需要处理它。

现在你描述的是对这两个数组进行bubbleSorting。为此,您需要一个包含它们的大数组。

int combinedLength = array1.length + array2.length;
int[] combined = new int[combinedLength];

for(int i = 0; i < array1.length; i++) {
    combined[i] = array1[i];
}
for(int i = 0; i < array2.length; i++) {
    combined[array1.length + i] = array2[i];
}

// now you can bubbleSort
bubbleSort(combined);
arrayPrint(combined);

理想情况下,您将该逻辑包装在合并方法中 - 这种特定方法利用数组和系统 类 为您完成一些提升。显然,如果需要,您可以使用上面的 "naive" 逻辑。

int[] merge(int[] a , int[] b) {
    int[] c = Arrays.copyOf(a, a.length + b.length);
    System.arraycopy(b,0,c,a.length,b.length);
    return c;
}

如果你也做一个获取数组的方法,像这样:

public int[] acquireArray(Scanner sc) {
    System.out.println("Length? ");
    int len = sc.nextInt();
    int[] arr = new int[len];
    for(int i = 0; i < len; i++) {
        System.out.println("Enter element " + (i+1) + ":");
        arr[i] = sc.nextInt();
    }
    return arr;
}

然后你的代码就变得非常非常干净了:

Scanner sc = new Scanner(System.in);
int[] a = acquireArray(sc);
int[] b = acquireArray(sc);
int[] c = merge(a,b);
bubbleSort(c);
arrayPrint(c);

我制作了一个驱动程序来测试这些想法,以确保它们都能正常工作。不过,我有点担心,因为你提到了递归。正如您在该驱动程序中所见,此处没有递归。另请注意,我采用了一些可能不允许的快捷方式(例如 System.arraycopyArrays.copyOfArrays.toString)。我只是想验证各种功能。该消息使用 1 索引,因为这是大多数人的想法。如果您输入 5 个元素,它们将为 1-5。你我都知道 Java 将它们存储为 0 索引,0-4。这只是品味和用户体验的问题。

import java.util.*;

public class BubbleSort {

    public static void main(String...args) {
        Scanner sc = new Scanner(System.in);

        int[] a = acquireArray(sc);
        int[] b = acquireArray(sc);
        int[] c = merge(a,b);
        bubbleSort(c);
        printArray(c);

    }

    public static int[] acquireArray(Scanner sc) {
        System.out.println("Length? ");
        int len = sc.nextInt();
        int[] arr = new int[len];
        for(int i = 0; i < len; i++) {
            System.out.println("Enter element " + (i+1) + ":");
            arr[i] = sc.nextInt();
        }
        return arr;
    }

    public static int[] merge(int[] a , int[] b) {
        int[] c = Arrays.copyOf(a, a.length + b.length);
        System.arraycopy(b,0,c,a.length,b.length);
        return c;
    }

    public static void bubbleSort(int[] a) {
        boolean swapped = true;
        int j = 0;
        while(swapped) {
            swapped = false;
            j++;
            for(int i = 0; i < a.length - j; i++) {
                if(a[i] > a[i+1]) {
                    int t = a[i];
                    a[i] = a[i+1];
                    a[i+1] = t;
                    swapped = true;
                }
            }
        }
    }

    public static void printArray(int[] a) {
        System.out.println(Arrays.toString(a));
    }

}

这就是我 运行 它

时得到的结果
C:\files\j>java BubbleSort
Length?
5
Enter element 1:
1
Enter element 2:
5
Enter element 3:
3
Enter element 4:
9
Enter element 5:
7
Length?
5
Enter element 1:
2
Enter element 2:
6
Enter element 3:
4
Enter element 4:
0
Enter element 5:
8
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]