java 使用整数数组的冒泡排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12251979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 08:11:09  来源:igfitidea点击:

BubbleSort using integer Array

javasortingbubble-sort

提问by Kanwaljeet Singh

I have been trying to implement Bubble Sort using simple integer array in java. However there seems to be some problem. Now i know that using ArrayList would be the best option and I would do that too. But why isnt it getting sorted with simple integer array.Here is the code

我一直在尝试使用 java 中的简单整数数组来实现冒泡排序。不过好像有点问题。现在我知道使用 ArrayList 将是最好的选择,我也会这样做。但是为什么不使用简单的整数数组对其进行排序。这是代码

package sort;

public class BubbleSort {

    int array[]={1,5,3,32,54,6,87,5,1};
    int temp=0;
public void enter(){
    for(int i=0;i<array.length;i++){
        for(int j=0;j<(array.length-i);j++){
            if(array[j]>=array[j+1]){


                temp=array[j];
                array[j]=array[j+1];
                array[j+1]=temp;
                }

        }
    }
}
public void show(){

    for(int i:array){
    System.out.println(i);
    }
}
public static void main(String str[]){

    new BubbleSort().Enter();
    new BubbleSort().Show();
}
}

Its producing the same array as entered. Nothing is getting changed. The difference between a simple array and an ArrayList or Vector ,is just that they offer dynamic time expansion of array size.Is there anything more to it? I mean does simple array creates a different instance every time it is manipulated, just like Strings? It does seem to do so here.

它产生与输入相同的数组。什么都没有改变。简单数组与 ArrayList 或 Vector 之间的区别在于,它们提供数组大小的动态时间扩展。还有更多吗?我的意思是简单数组在每次操作时都会创建一个不同的实例,就像字符串一样?在这里似乎确实如此。

回答by Bharat Sinha

Because you are sorting one instance and showing another.

因为您正在对一个实例进行排序并显示另一个实例。

new BubbleSort().Enter();
new BubbleSort().Show();

Use

利用

BubbleSort bubbleSort = new BubbleSort();
bubbleSort.Enter();
bubbleSort.Show();

Also you should rename Enter()to enter()and Show()to show()to say the least.

此外,Enter()enter()Show()show()至少应该重命名to

回答by Jamie

The problem is that you're not assigning a name to the instantiation of your BubbleSort class.

问题是您没有为 BubbleSort 类的实例化分配名称。

new BubbleSort().Enter();
new BubbleSort().Show();

Your code creates a new BubbleSort class, and then sorts it. And then it creates another new (and completely separate) BubbleSort class, and displays that one instead - and it hasn't been sorted.

您的代码创建一个新的 BubbleSort 类,然后对其进行排序。然后它创建另一个新的(完全独立的) BubbleSort 类,并显示那个 - 它没有被排序。

You want to give a name to your variable, so you can sort it and then display it, like this:

您想为变量命名,以便可以对其进行排序然后显示它,如下所示:

BubbleSort myBubbleSort = new BubbleSort();
myBubbleSort.Enter();
myBubbleSort.Show();

As a side note (and as pointed out in SiB's answer), you may also want to check out the Java Naming Conventions. Following these conventions makes your code more legible to other Java programmers, and includes things like using lowerCamelCasefor method names, and UpperCamelCasefor class names.

作为旁注(正如在 SiB 的回答中指出的那样),您可能还想查看Java Naming Conventions。遵循这些约定使您的代码对其他 Java 程序员更清晰,并且包括lowerCamelCase用于方法名称和UpperCamelCase类名称的内容。

回答by ibondre

Because you are creating two different BubbleSort Objects, sorting the first one and displaying a different one.

因为您正在创建两个不同的 BubbleSort 对象,对第一个对象进行排序并显示不同的对象。

It should have been....

本来应该是......

public static void main(String str[]){

    BubbleSort sort = new BubbleSort();
    sort.Enter();
    sort.Show():

}

回答by Eduardo Andrade

And the correct BubbleSort code is:

正确的 BubbleSort 代码是:

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

        }
    }

I hope it helps someone else looking for it.

我希望它可以帮助其他人寻找它。