似乎无法弄清楚为什么冒泡排序在 java 中不起作用

Can't seem to figure out why bubble sort is not working in java

public static void main(String[] args) {

    int a[]={13,33,1,32,8,10,11,6};
    bubbleSort(a);


}

public static void bubbleSort(int []a){
int temp=0;
    int n= a.length;
    for(int i=0;i<n;i++ ){

        for(int j=1; j<n-1;j++){  

            if(a[j-1]>a[j]){

                temp=a[j-1];
                a[j-1]=a[j];
                a[j]=temp;

            }
        }System.out.println(a[i]);
    }
}

我似乎不明白我如何设法从算法中获得这种随机排序!? 1 和 8 根本没有出现,13 出现了 3 次。

结果: 13 13 10 11 13 32 33 6

j 应该一直迭代到 n(直到 n-1)。

变化:

for(int j=1; j<n-1; j++){  

至:

for(int j=1; j<n; j++){  

你的冒泡排序会起作用。

这里有两个主要问题:

  1. 正如@alfasin所说"j should iterate all the way up until n (not until n-1)."

  2. System.out.println应该在所有操作执行完之后执行,而不是在排序过程中执行。

以下应该有效:

public class Main {
    public static void main(String[] args) {
        int a[]={13,33,1,32,8,10,11,6};


        int result[] = bubbleSort(a);

        for(int i:result)
            System.out.println(i);  
    }

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

二级迭代提前停止了一步。而且您在错误的阶段打印数组元素。

public static void bubbleSort(int []a){
    int temp=0;
    int n= a.length;
    for(int i=0;i<n;i++ ){
        for(int j=1; j<n;j++){
            if(a[j-1] > a[j]){
                temp=a[j-1]; a[j-1]=a[j]; a[j]=temp;
            }
        }
    }
    for(int i: a) {
        System.out.print(i+" ");
    }
}

输出不是很有用,因为您在排序过程中打印了值。

for(int i=0;i<n;i++ ){
     System.out.println(a[i]);
}

在结果的末尾使用它。 @alfasin 已经指出了另一个错误。

除了 alfashin 已经说过的,你的循环应该转到 n,而不是 n-1 是你不输出你的最终结果,而是输出校准步骤 i 的中间结果:

System.out.println(a[i]);

确保排序完成后输出

我发现了 2 个错误。

  • 我删除了你的打印语句并将其放在循环之外。
  • 你的第二个循环看起来像这样。 for(int j = 1; j < n - 1; j++){ 我用这个代码行替换了。 for (int j = 1; j < (n - i); j++) {

请尝试以下代码。

public class BubbleSort{

   public static void main(String[] args) {

      int a[] = {13, 33, 1, 32, 8, 10, 11, 6};
      bubbleSort(a);

   }

   public static void bubbleSort(int[] a) {
      int temp = 0;
      int n = a.length;

      for (int i = 0; i < n; i++) {
         for (int j = 1; j < (n - i); j++) {
            if (a[j - 1] > a[j]) {
               //swap elements  
               temp = a[j - 1];
               a[j - 1] = a[j];
               a[j] = temp;
            }

         }

      }
      // print array after sorting. 
      for (int i = 0; i < a.length; i++) {
         System.out.print(a[i] + " ");
      }
   }

}

除了大家已经提到的两个错误之外,还值得注意的是,您关于如何执行冒泡排序的逻辑不是最优的。

您使用的是外部 for 循环,它假定您需要执行内部循环的次数一致。但实际上这完全取决于您正在使用的列表。

我将使用您代码的这个固定版本来演示它:

public static void main(String[] args) {
    int a[] = {13, 33, 1, 32, 8, 10, 11, 6};
    bubbleSort(a);

}

public static void bubbleSort(int[] a) {
    int temp;
    int n = a.length;
    for (int i = 0; i < n; i++) {

        for (int j = 1; j < n; j++) {

            if (a[j - 1] > a[j]) {

                temp = a[j - 1];
                a[j - 1] = a[j];
                a[j] = temp;

            }
        }
    }
    System.out.println(Arrays.toString(a));
}

现在外层 for 循环 运行s n 次,所以 8 次。你得到一个排序列表作为输出。

[1, 6, 8, 10, 11, 13, 32, 33]

但是如果 运行 少于 8 次会怎样?尝试将 n 替换为 6。输出:

[1, 6, 8, 10, 11, 13, 32, 33]

相同的结果,这意味着您 运行 在一个已经排序的列表上排序了两次。如果你有一个已经排序的 1000 个整数列表,这可能是个问题,因为你将白白循环 1000 次。

来自维基百科:"The only significant advantage that bubble sort has over most other implementations [...] is that the ability to detect that the list is sorted efficiently is built into the algorithm. When the list is already sorted (best-case), the complexity of bubble sort is only O(n)."

所以你基本上破坏了使用冒泡排序的唯一理由。

你会想要使用其他类型的循环,祝你好运。