Массивы пузырьковой сортировки

Итак, я немного отредактировал его и получаю почти именно то, что хочу, и я пробовал несколько разных вещей для счетчика, но у меня просто недостаточно понимания, как его включить. Я хочу, чтобы счетчик вел кумулятивный подсчет количества свопов, и я хочу иметь возможность печатать это количество при печати каждой новой строки. Вот что у меня без счетчика;

          static void bubbleSort(int[] myArray) {
    int n = myArray.length;
    int temp = 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: ");
        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; 
            }}}}
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);}

Вот пример того, что я получаю без счета:

Введите слот 0: 96
Введите слот 1:55
Введите слот 2: 116
Введите слот 3: 4
Введите слот 4: 56
Введите слот 5: 322
Введите слот 6:45
Введите слот 7: 884
Введите слот 8: 3
Введите слот 9: 111111
96 | 55 | 116 | 4 | 56 | 322 | 45 | 884 | 3 | 111111 | Кол-во свопов:
55 | 96 | 4 | 56 | 116 | 45 | 322 | 3 | 884 | 111111 | Кол-во свопов:
55 | 4 | 56 | 96 | 45 | 116 | 3 | 322 | 884 | 111111 | Кол-во свопов:
4 | 55 | 56 | 45 | 96 | 3 | 116 | 322 | 884 | 111111 | Кол-во свопов:
4 | 55 | 45 | 56 | 3 | 96 | 116 | 322 | 884 | 111111 | Кол-во свопов:
4 | 45 | 55 | 3 | 56 | 96 | 116 | 322 | 884 | 111111 | Кол-во свопов:
4 | 45 | 3 | 55 | 56 | 96 | 116 | 322 | 884 | 111111 | Кол-во свопов:
4 | 3 | 45 | 55 | 56 | 96 | 116 | 322 | 884 | 111111 | Кол-во свопов:
3 | 4 | 45 | 55 | 56 | 96 | 116 | 322 | 884 | 111111 | Кол-во свопов:
3 | 4 | 45 | 55 | 56 | 96 | 116 | 322 | 884 | 111111 | Кол-во свопов:

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);
    }
}

Пузырьковая сортировка с выводом

      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;
    boolean swapped;
    // repeat the passes through the list until
    // all the elements are in the correct order
    do {
        // output the beginning of the pass and increase the count of passes
        System.out.print((passes == 0 ? "<pre>" : "<br>") + "Pass: " + passes++);
        swapped = false;
        // pass through the list 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 count of swaps
                System.out.print(outputSwap(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 outputSwap(int[] arr, int e1, int e2, int count) {
    StringBuilder sb = new StringBuilder("<br>");
    for (int i = 0; i < arr.length; i++) {
        if (i == e1 || i == e2) {
            sb.append("<b>").append(arr[i]).append("</b>");
        } else {
            sb.append(arr[i]);
        }
        sb.append(" ");
    }
    return sb.append("| ").append(count).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 76 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 32 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
Другие вопросы по тегам