简体   繁体   中英

Java Beginner: Array Bubble Sort

The main method creates an array of random integers then calls the ascending and descending methods to sort the array. It seems that when I call the ascending and descending methods it alters the value of the original array. The output I receive when printing is three arrays all sorted in descending order.

public static void main(String[] args){

    Random random = new Random();
    Scanner myScan = new Scanner(System.in);

    System.out.println("How many random numbers should be created?");

    int size = myScan.nextInt();
    int[] array = new int[size];

    for(int i = 0; i<array.length; i++){    
        array[i] = random.nextInt(256);
    }

    int[] ascending = BubbleSort.ascending(array);

    int[] descending = BubbleSort.descending(array);

    System.out.print("Original array: ");
    for(int i = 0; i< array.length; i++){
        System.out.print(array[i] + " ");
    }
    System.out.println();   

    System.out.print("Array in ascending order: ");
    for(int i =0; i< ascending.length; i++){
        System.out.print(ascending[i] + " ");
    }
    System.out.println();


    System.out.print("Array in descending order: ");
    for(int i = 0; i<descending.length; i++){
        System.out.print(descending[i] + " ");
    }
    System.out.println();
}

Here are the two sorting methods.

public static int[] ascending(int[] a){

    int temp;

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

    return(a);
}

public static int[] descending(int[] b){

    int temp;

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

    return(b);
}

Instead of

int[] ascending = BubbleSort.ascending(array);

I thought to try

int[] ascending = new int[array.length];
ascending = BubbleSort.ascending(array);

But I receive the same results. Sorry for being such a programming newbie, and thanks for any guidance!

I haven't looked at the correctness of your sorts, but what I can tell you is that both methods take in an array and do modify it directly.

public static int[] descending(int[] b){

    int temp;

    for(int i = 0; i < b.length - 1; i++){
        for(int j = 0; j < b.length -1; j++){   
            if(b[j] < b[j+1]){
                temp = b[j];
                // both of these calls CHANGE the array
                b[j] = b[j+1];                     
                b[j+1] = temp;
            }
        }
    }

    return(b);
}

Takes in an array (b) and returns that array sorted (return b). That means that whatever array you pass into this function will be changed.

 int[] array = new int[size];

So that array which is getting passed into:

int[] ascending = BubbleSort.ascending(array);

int[] descending = BubbleSort.descending(array);

First get's created, then MODIFIED once by ascending, then modified AGAIn by descending.

What you would need to do to avoid this is to create a copy of this array, or perform your sort in a manner which does not affect the original array.

See this question: Deep copy, shallow copy, clone

For a discussion of copying, deep copying and cloning in java. This should clarify some issues you're running into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM