简体   繁体   English

Java初学者:数组冒泡排序

[英]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. main方法创建一个随机整数数组,然后调用升序和降序方法对数组进行排序。 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). 接收数组(b)并返回已排序的数组(返回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. 首先创建,然后通过升序修改一次,然后通过降序修改AGAIn。

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. 有关在Java中进行复制,深度复制和克隆的讨论。 This should clarify some issues you're running into. 这应该可以澄清您遇到的一些问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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