【问题标题】:Bubble sort output is incorrect冒泡排序输出不正确
【发布时间】:2018-12-06 22:26:18
【问题描述】:

这是我的冒泡排序代码,但我很困惑为什么输出只显示125

int secondArray[] = {0, 1, 5, 2};
int num;

for (int i = 1; i < secondArray.length; i++) {
    for (int j = 0; j < secondArray.length - i; j++) {
        if (secondArray[j] > secondArray[j + 1]) {
            num = secondArray[j];
            secondArray[j] = secondArray[j + 1];
            secondArray[j + 1] = num;
        }
    }
    System.out.print(secondArray[i]);
}

【问题讨论】:

    标签: bubble-sort java


    【解决方案1】:

    外层do-while-loop重复遍历数组,直到所有元素的顺序正确,内层for-loop遍历数组并比较相邻元素。

    public static void bubbleSort(int[] arr) {
        boolean swapped;
        // repeat the passes through the array until
        // all the elements are in the correct order
        do {
            swapped = false;
            // pass through the array and
            // compare adjacent elements
            for (int i = 0; i < arr.length - 1; i++) {
                // if this element is greater than
                // the next one, then swap them
                if (arr[i] > arr[i + 1]) {
                    int temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                    swapped = true;
                }
            }
            // if there are no swapped elements at the
            // current pass, then this is the last pass
        } while (swapped);
    }
    
    public static void main(String[] args) {
        int[] arr = {6, 1, 5, 8, 4, 3, 9, 2, 0, 7};
        System.out.println(Arrays.toString(arr));
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    

    输出:

    [6, 1, 5, 8, 4, 3, 9, 2, 0, 7]
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    

    另见:Bubble sort with step-by-step outputJava 8

    【讨论】:

      【解决方案2】:
      public class Example {
          public static void sortArray(int[] x) {
              for (int j = 0; j < x.length - 1; j++) {
                  for (int i = 0; i < x.length - 1; i++) {
                      if (x[i] > x[i + 1]) {
                          int temp = x[i];
                          x[i] = x[i + 1];
                          x[i + 1] = temp;
                      }
                  }
              }
          }
      
          public static void main(String[] args) {
              int[] x = {32, 98, 35, 76, 26, 89, 1, 46, 21, 7};
              System.out.println(Arrays.toString(x));
              sortArray(x);
              System.out.println(Arrays.toString(x));
          }
      }
      

      输出:

      [32, 98, 35, 76, 26, 89, 1, 46, 21, 7]
      [1, 7, 21, 26, 32, 35, 46, 76, 89, 98]
      

      【讨论】:

        【解决方案3】:

        请运行以下代码:

        int secondArray[] = {0, 1, 5, 2};
        int num;
        
        for (int i = 0; i < secondArray.length - 1; i++) {
            for (int j = 0; j < secondArray.length - 1 - i; j++) {
                if (secondArray[j] > secondArray[j + 1]) {
                    num = secondArray[j];
                    secondArray[j] = secondArray[j + 1];
                    secondArray[j + 1] = num;
                }
            }
        }
        for (int number : secondArray) {
            System.out.print(number);
        }
        

        【讨论】:

          【解决方案4】:

          冒泡排序的完整代码:

          class BubbleSort {
              void bubbleSort(int array[]) {
                  int n = array.length;
                  for (int i = 0; i < n - 1; i++) {
                      for (int j = 0; j < n - i - 1; j++) {
                          if (array[j] > array[j + 1]) {
                              int temp = array[j];
                              array[j] = array[j + 1];
                              array[j + 1] = temp;
                          }
                      }
                  }
              }
          
              void printArray(int array[]) {
                  for (int i = 0; i < array.length; ++i) {
                      System.out.print(array[i] + " ");
                  }
                  System.out.println();
              }
          
              // Main method
              public static void main(String args[]) {
                  BubbleSort bubbleSort = new BubbleSort();
                  int array[] = {64, 34, 25, 12, 22, 11, 90};
                  System.out.print("Original array : ");
                  bubbleSort.printArray(array);
                  bubbleSort.bubbleSort(array);
                  System.out.print("Sorted array : ");
                  bubbleSort.printArray(array);
              }
          }
          

          输出:

          Original array : 64 34 25 12 22 11 90 
          Sorted array : 11 12 22 25 34 64 90 
          

          【讨论】:

            【解决方案5】:

            冒泡排序 - Java 代码

            import java.util.*;
            class bubblesort {
                public static void main(String[] args) {
                    int ar[] = {34, 12, 45, 3, 6, 7, 20};
                    sort(ar);
                    System.out.print("After sort :  ");
                    for (int j = 0; j < ar.length; j++) {
                        System.out.print(ar[j] + " ");
                    }
                }
            
                public static void sort(int[] arr) {
                    int temp = 0;
                    for (int i = 0; i < arr.length; i++) {
                        for (int j = 0; j < (arr.length - 1 - i); j++) {
                            if (arr[j] > arr[j + 1]) {
                                temp = arr[j];
                                arr[j] = arr[j + 1];
                                arr[j + 1] = temp;
                            }
                        }
                    }
                }
            }
            

            【讨论】:

              【解决方案6】:

              看看这个。

              int secondArray[] = {0, 1, 5, 2};
              int temp;
              
              for (int i = 0, k = secondArray.length; i < secondArray.length / 2; i++) {
                  temp = secondArray[i];
                  secondArray[i] = secondArray[--k];
                  secondArray[k] = temp;
                  System.out.println(secondArray[0] + " " + secondArray[1]
                          + " " + secondArray[2] + " " + secondArray[3]);
              }
              

              【讨论】:

                【解决方案7】:

                这是因为您从 1 迭代 -> int i = 1; 但数组从 0 开始,所以 System.out.print(secondArray[i]); 永远没有机会显示第一个元素。

                【讨论】:

                • 根据问题陈述的逻辑,如果用户从i = 0开始,他/她会得到“java.lang.ArrayIndexOutOfBoundsException: 4”
                • @Deepak 是的,但我没有告诉他这样做。他可以在循环后打印最后一个数字 - 或全部。
                猜你喜欢
                • 1970-01-01
                • 2016-02-18
                • 2022-06-10
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2018-08-19
                • 1970-01-01
                相关资源
                最近更新 更多