【问题标题】:Bubble sort with output带输出的冒泡排序
【发布时间】:2021-06-05 04:33:46
【问题描述】:

所以我对它进行了一些编辑,几乎得到了我想要的东西。我现在遇到的唯一问题是我得到了一行我不想要的输出。我觉得这里的解决方法很简单,但我的大脑现在已经炸了。

static void bubbleSort(int[] myArray) {
    int n = myArray.length;
    int temp = 0;
    int counter = 0;
    for (int i = 0; i < n; i++) {
        int k;
        for (k = 0; k < n; k++) {
            System.out.print(myArray[k] + "|");
        }
        System.out.println(" Num swaps: " + counter);
        for (int j = 1; j < (n - i); j++) {
            if (myArray[j - 1] > myArray[j]) {
                //swap elements
                temp = myArray[j - 1];
                myArray[j - 1] = myArray[j];
                myArray[j] = temp;
                counter++;
            }
        }
    }
}
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int[] myArray = new int[10];
    for (int i = 0; i < 10; i++) {
        System.out.print("Enter slot " + i + ": ");
        myArray[i] = sc.nextInt();
    }
    bubbleSort(myArray);
}

这是我得到的一个例子:

Enter slot 0: 10
Enter slot 1: 9
Enter slot 2: 8
Enter slot 3: 7
Enter slot 4: 6
Enter slot 5: 5
Enter slot 6: 4
Enter slot 7: 3
Enter slot 8: 2
Enter slot 9: 1
10|9|8|7|6|5|4|3|2|1| Num swaps: 0
9|8|7|6|5|4|3|2|1|10| Num swaps: 9
8|7|6|5|4|3|2|1|9|10| Num swaps: 17
7|6|5|4|3|2|1|8|9|10| Num swaps: 24
6|5|4|3|2|1|7|8|9|10| Num swaps: 30
5|4|3|2|1|6|7|8|9|10| Num swaps: 35
4|3|2|1|5|6|7|8|9|10| Num swaps: 39
3|2|1|4|5|6|7|8|9|10| Num swaps: 42
2|1|3|4|5|6|7|8|9|10| Num swaps: 44
1|2|3|4|5|6|7|8|9|10| Num swaps: 45

输出的第一行只是重复用户输入的内容并说 0 交换。我不想那样。

【问题讨论】:

    标签: java arrays counter swap bubble-sort


    【解决方案1】:

    具有两个嵌套流的算法Bubble sort with step-by-step output Java 8


    逐步输出的冒泡排序

    外层do-while-loop重复直到数组排序完毕,内层for-loop遍历数组,交换无序的相邻元素。输出是内循环中的 交换 元素,外循环按 passes 分组。

    public static void main(String[] args) {
        int[] arr = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
        bubbleSort(arr);
    }
    
    public static void bubbleSort(int[] arr) {
        // counters
        int passes = 0, swaps = 0;
        // marker
        boolean swapped;
        // repeat the passes through the array until
        // all the elements are in the correct order
        do {
            // output the beginning of the pass and increase the counter of passes
            System.out.print((passes == 0 ? "<pre>" : "<br>") + "Pass: " + passes++);
            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;
                    // output the array and increase the counter of swaps
                    System.out.print(outputSwapped(arr, i, i + 1, swaps++));
                }
            }
            // if there are no swapped elements at the
            // current pass, then this is the last pass
        } while (swapped);
        // output total
        System.out.print("<br>Total: Passes=" + passes);
        System.out.println(", swaps=" + swaps + "</pre>");
    }
    
    static String outputSwapped(int[] arr, int e1, int e2, int counter) {
        StringBuilder sb = new StringBuilder("<br>");
        for (int i = 0; i < arr.length; i++) {
            if (i == e1 || i == e2) {
                // swapped elements are in bold
                sb.append("<b>").append(arr[i]).append("</b>");
            } else {
                // other elements
                sb.append(arr[i]);
            }
            sb.append(" ");
        }
        return sb.append("| ").append(counter).toString();
    }
    

    输出:

    通过:0
    9 10 8 7 6 5 4 3 2 1 | 0
    9 8 10 7 6 5 4 3 2 1 | 1
    9 8 7 10 6 5 4 3 2 1 | 2
    9 8 7 6 10 5 4 3 2 1 | 3
    9 8 7 6 5 10 4 3 2 1 | 4
    9 8 7 6 5 4 10 3 2 1 | 5
    9 8 7 6 5 4 3 10 2 1 | 6
    9 8 7 6 5 4 3 2 10 1 | 7
    9 8 7 6 5 4 3 2 1 10 | 8
    通过:1
    8 9 7 6 5 4 3 2 1 10 | 9
    8 7 9 6 5 4 3 2 1 10 | 10
    8 7 6 9 5 4 3 2 1 10 | 11
    8 7 6 5 9 4 3 2 1 10 | 12
    8 7 6 5 4 9 3 2 1 10 | 13
    8 7 6 5 4 3 9 2 1 10 | 14
    8 7 6 5 4 3 2 9 1 10 | 15
    8 7 6 5 4 3 2 1 9 10 | 16
    通过:2
    7 8 6 5 4 3 2 1 9 10 | 17
    7 6 8 5 4 3 2 1 9 10 | 18
    7 6 5 8 4 3 2 1 9 10 | 19
    7 6 5 4 8 3 2 1 9 10 | 20
    7 6 5 4 3 8 2 1 9 10 | 21
    7 6 5 4 3 2 8 1 9 10 | 22
    7 6 5 4 3 2 1 8 9 10 | 23
    通过:3
    6 7 5 4 3 2 1 8 9 10 | 24
    6 5 7 4 3 2 1 8 9 10 | 25
    6 5 4 7 3 2 1 8 9 10 | 26
    6 5 4 3 7 2 1 8 9 10 | 27
    6 5 4 3 2 7 1 8 9 10 | 28
    6 5 4 3 2 1 7 8 9 10 | 29
    通过:4
    5 6 4 3 2 1 7 8 9 10 | 30
    5 4 6 3 2 1 7 8 9 10 | 31
    5 4 3 6 2 1 7 8 9 10 | 32
    5 4 3 2 6 1 7 8 9 10 | 33
    5 4 3 2 1 6 7 8 9 10 | 34
    通过:5
    4 5 3 2 1 6 7 8 9 10 | 35
    4 3 5 2 1 6 7 8 9 10 | 36
    4 3 2 5 1 6 7 8 9 10 | 37
    4 3 2 1 5 6 7 8 9 10 | 38
    通过:6
    3 4 2 1 5 6 7 8 9 10 | 39
    3 2 4 1 5 6 7 8 9 10 | 40
    3 2 1 4 5 6 7 8 9 10 | 41
    通过:7
    2 3 1 4 5 6 7 8 9 10 | 42
    2 1 3 4 5 6 7 8 9 10 | 43
    通过:8
    1 2 3 4 5 6 7 8 9 10 | 44
    通过:9
    总计:通过=10,交换=45

    另见:Bubble sort output is incorrect

    【讨论】:

      【解决方案2】:

      只是改变了 for 循环的位置。希望这是您真正想要的输出:)。

      static void bubbleSort(int[] myArray) {
          int n = myArray.length;
          int temp = 0;
          int counter = 0;
          for (int i = 0; i < n; i++) {
              for (int j = 1; j < (n - i); j++) {
                  if (myArray[j - 1] > myArray[j]) {
                      // swap elements
                      temp = myArray[j - 1];
                      myArray[j - 1] = myArray[j];
                      myArray[j] = temp;
                      counter++;
                  }
              }
              int k;
              for (k = 0; k < n; k++) {
                  System.out.print(myArray[k] + "|");
              }
              System.out.println(" Num swaps: " + counter);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-02-18
        • 1970-01-01
        • 2016-01-19
        • 2023-04-07
        • 2018-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-17
        相关资源
        最近更新 更多