2021年3月6日星期六

Output from sorting

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]+"|");      }        // enter code here      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. Brain is fried, please help with this.

https://stackoverflow.com/questions/66512108/output-from-sorting March 07, 2021 at 08:33AM

没有评论:

发表评论