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

337 views Asked by At

I'm writing a code that ask at the user to insert the numbers of the array and then write each numbers, do the same thing in another array, and at the end compare the first array with the second array and print out the bubble sort of all numbers, so a kind of bubble sort for the first and second array togheter. I wrote this below, but I don t know how to compare with one method the 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));
}

}

1

There are 1 answers

0
corsiKa On BEST ANSWER

You mention in the comments that you've already solved bubblesort. So I'm going to assume you have a method with the signature void bubbleSort(int[] arr).

Your code shows you understand how to acquire an array from the user, so we don't need to handle that.

Now what you're describing is bubbleSorting these two arrays. To do this, you need one -big- array that holds them both.

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

Ideally you wrap that logic in a merge method - this particular method leverages the Arrays and System classes to do some of the lifting for you. Obviously you could use the "naive" logic above if you want.

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

If you also make a method that acquires an array, like this:

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

Then your code becomes very, very clean:

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

I made a driver to test each of these ideas out to make sure they all work. I am a bit concerned, though, because you mention recursion. As you can see in this driver, there is no recursion here. Also be aware that I take a number of shortcuts that are probably not allowed (such as System.arraycopy, Arrays.copyOf, and Arrays.toString). I just wanted to validate the various pieces of functionality. The message uses 1 indexing because that's what most people think in. If you enter 5 elements, they'll be 1-5. You and I know Java stores them 0 indexed, 0-4. It's just a matter of taste and UX.

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

}

And here's what I get when I run it

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]