Java 冒泡排序是否再次返回相同的数组?

Java 冒泡排序是否再次返回相同的数组?,java,arrays,sorting,Java,Arrays,Sorting,有人能解释为什么这个冒泡排序在我的代码中不起作用吗。我想这很容易分类。也许不是所有的都正确,但仍然有一些排序,但它只是返回相同的数组 import java.util.Random; public class Sorts { public static void main(String[] args) { // TODO Auto-generated method stub //Set array to be sorted length here!

有人能解释为什么这个冒泡排序在我的代码中不起作用吗。我想这很容易分类。也许不是所有的都正确,但仍然有一些排序,但它只是返回相同的数组

import java.util.Random;


public class Sorts {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        //Set array to be sorted length here!
        int listLength = 20; 

        //Declares Array
        int[] toBeSortedArray = new int[listLength];
        int[] sortedArray = new int[listLength];

        //fills Array with random numbers
        for (int i = 0; i < listLength; i++)
        {
            Random rand = new Random();
            toBeSortedArray[i] = rand.nextInt(100);
        }


        //Passing toBeSortedArray to function
        sortedArray = SwapSort(toBeSortedArray, listLength);



        //Testing the filling of Array - *hint* select all comment lines and select "Toggle Block Comment" 
        for (int i = 0; i <listLength; i++)
        {
            System.out.print(toBeSortedArray[i] + ", ");
        }
        System.out.println();

        for (int i = 0; i <listLength; i++)
        {
            System.out.print(sortedArray[i] + ", ");
        }


    }

    public static int[] SwapSort(int[] array, int length)
    {
        int temp;
        for (int i = 0; i < length; i++)
        {
            for (int j = 1; j < length - 1; j++)
            {
                if (array[i] > array[j])
                {                       
                    temp = array[i];
                    array[i] = array[j+1];
                    array[j+1] = temp;
                }

            }
        }

        return array;
    }
}

SwapSort
中的内部循环应以
intj=i+1开始;j
(和外部循环中的
i
),想想当算法中
j
1
i
2
时会发生什么。此外,交换应该发生在正在比较的元素上。像

公共静态int[]SwapSort(int[]数组,int长度){
内部温度;
对于(int i=0;i数组[j]){
温度=阵列[i];
数组[i]=数组[j];
数组[j]=温度;
}
}
}
返回数组;
}

您在
SwapSort
中的内部循环应以
int j=i+1开头;j
(和外部循环中的
i
),想想当算法中
j
1
i
2
时会发生什么。此外,交换应该发生在正在比较的元素上。像

公共静态int[]SwapSort(int[]数组,int长度){
内部温度;
对于(int i=0;i数组[j]){
温度=阵列[i];
数组[i]=数组[j];
数组[j]=温度;
}
}
}
返回数组;
}
三件事

首先,您交换了错误的元素

if (array[i] > array[j]) {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}
必须交换元素数组[i]和数组[j]

您的内部循环必须从
j=i+1
开始,而不是
1
开始,并且应该达到
长度

第三

由于您在调用函数后在代码中打印这两个数组,因此这两个数组都将给出相同的输出,因为java通过引用传递数组,并且您的原始数组也会被修改。因此,即使在原始代码中发生了交换,也会得到相同的输出

完整代码

class Sorts {
    public static void main(String[] args) {
        //Set array to be sorted length here!
        int listLength = 20;

        //Declares Array
        int[] toBeSortedArray = new int[listLength];
        int[] sortedArray = new int[listLength];

        //fills Array with random numbers
        for (int i = 0; i < listLength; i++) {
            Random rand = new Random();
            toBeSortedArray[i] = rand.nextInt(100);
        }

        for (int i = 0; i < listLength; i++) {
            System.out.print(toBeSortedArray[i] + ", ");
        }

        //Passing toBeSortedArray to function
        sortedArray = SwapSort(toBeSortedArray, listLength);
    }

    public static int[] SwapSort(int[] array, int length) {
        int temp;
        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length; j++) {
                if (array[i] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }

            }
        }
        System.out.println("");
        for (int i = 0; i < length; i++) {
            System.out.print(array[i] + ", ");
        }
        System.out.println();
        return array;
    }
}
类排序{
公共静态void main(字符串[]args){
//在这里设置数组的排序长度!
int listLength=20;
//声明数组
int[]toBeSortedArray=新int[listLength];
int[]sortedArray=新int[listLength];
//用随机数填充数组
for(int i=0;i数组[j]){
温度=阵列[i];
数组[i]=数组[j];
数组[j]=温度;
}
}
}
System.out.println(“”);
for(int i=0;i
三件事

首先,您交换了错误的元素

if (array[i] > array[j]) {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}
必须交换元素数组[i]和数组[j]

您的内部循环必须从
j=i+1
开始,而不是
1
开始,并且应该达到
长度

第三

由于您在调用函数后在代码中打印这两个数组,因此这两个数组都将给出相同的输出,因为java通过引用传递数组,并且您的原始数组也会被修改。因此,即使在原始代码中发生了交换,也会得到相同的输出

完整代码

class Sorts {
    public static void main(String[] args) {
        //Set array to be sorted length here!
        int listLength = 20;

        //Declares Array
        int[] toBeSortedArray = new int[listLength];
        int[] sortedArray = new int[listLength];

        //fills Array with random numbers
        for (int i = 0; i < listLength; i++) {
            Random rand = new Random();
            toBeSortedArray[i] = rand.nextInt(100);
        }

        for (int i = 0; i < listLength; i++) {
            System.out.print(toBeSortedArray[i] + ", ");
        }

        //Passing toBeSortedArray to function
        sortedArray = SwapSort(toBeSortedArray, listLength);
    }

    public static int[] SwapSort(int[] array, int length) {
        int temp;
        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length; j++) {
                if (array[i] > array[j]) {
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }

            }
        }
        System.out.println("");
        for (int i = 0; i < length; i++) {
            System.out.print(array[i] + ", ");
        }
        System.out.println();
        return array;
    }
}
类排序{
公共静态void main(字符串[]args){
//在这里设置数组的排序长度!
int listLength=20;
//声明数组
int[]toBeSortedArray=新int[listLength];
int[]sortedArray=新int[listLength];
//用随机数填充数组
for(int i=0;i数组[j]){
温度=阵列[i];
数组[i]=数组[j];
数组[j]=温度;
}
}
}
System.out.println(“”);
for(int i=0;i
您的输出与输入不同,相反:您正在更改传入的数组,因此您正在修改输入。你的算法坏了。但是,如果你的算法有效,它们仍然是相同的,但已排序。你的输出与输入不同,而是相反:你正在更改传入的数组,因此你正在修改输入。你的算法坏了。但是,如果你的算法有效,它们仍然是相同的,但排序。为什么要导入java.lang。是我