Java初学者: Array Bubble Sort

标签 java arrays sorting

main方法创建一个随机整数数组,然后调用升序和降序方法对数组进行排序。似乎当我调用升序和降序方法时,它会改变原始数组的值。打印时收到的输出是三个数组,全部按降序排序。

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

以下是两种排序方法。

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

而不是

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

我想尝试一下

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

但我收到了相同的结果。很抱歉成为这样的编程新手,感谢您的指导!

最佳答案

我没有看过你的排序的正确性,但我可以告诉你的是,这两种方法都接受一个数组并直接修改它。

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

接收一个数组 (b) 并返回已排序的数组(返回 b)。这意味着您传递给此函数的任何数组都将发生更改。

 int[] array = new int[size];

因此该数组被传递到:

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

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

创建第一个 get,然后按升序修改一次,然后按降序修改 AGAIn。

为了避免这种情况,您需要做的是创建该数组的副本,或者以不影响原始数组的方式执行排序。

查看这个问题:Deep copy, shallow copy, clone

关于 Java 中的复制、深复制和克隆的讨论。这应该可以澄清您遇到的一些问题。

关于Java初学者: Array Bubble Sort,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20618126/

相关文章:

java - MigLayout表单类型结构问题

java - 非常基础 - Java 数组作为类变量

python - 从姓名列表中生成学生对,每周新对

C程序 'stuck'接受输入后

java - 如何清除 Junit 中提交的 afterClass 实例?

java - 从 JTree 中的文本文件动态填充树模型

java - 处理任务的应用程序设计

使用指针检查字符数组中的数字,在 c 中给出错误,给出运行时错误

c - 返回一个数组

JavaScript 按嵌套对象降序对数组进行排序