Bubble sort with output
Asked Answered
D

2

0

So I have edited it some and am getting almost exactly what I want. The only problem I am having now is that I am getting a line of output that I don't want. I feel like the fix here is simple but my brain is fried right now.

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

Here is an example of what I get:

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

That first line of output where it just repeats what the user input and says 0 swaps. I don't want that.

Dislimn answered 6/3, 2021 at 20:38 Comment(0)
H
1

Just changed the position of the for loops. Hope this is the output you actually want :).

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);
    }
}
Hiatt answered 5/4, 2021 at 20:29 Comment(0)
D
0

Algorithm with two nested streams: Bubble sort with step-by-step output Java 8


Bubble sort with step-by-step output

The outer do-while-loop repeats until the array is sorted, and the inner for-loop passes through the array, swapping the unordered adjacent elements. The output is the swapped elements in the inner loop, grouped by passes in the outer loop.

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

Output:

Pass: 0
9 10 8 7 6 5 4 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7 8 9 10 | 41
Pass: 7
2 3 1 4 5 6 7 8 9 10 | 42
2 1 3 4 5 6 7 8 9 10 | 43
Pass: 8
1 2 3 4 5 6 7 8 9 10 | 44
Pass: 9
Total: Passes=10, swaps=45

See also: Bubble sort output is incorrect

Distress answered 6/3, 2021 at 20:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.