How to make Bubble Sort in Java to output the sorted numbers?

前端 未结 4 753
野趣味
野趣味 2021-01-26 12:07

This is my code for the Bubble Sort. I cannot get the actual sorted values to output. The program reads the inputted numbers, but does not print it sorted. I\'m not sure what I

4条回答
  •  再見小時候
    2021-01-26 12:43

    Try this Bubble sort :

    private static void BubbleSort(int[] num) {
     for (int i = 0; i < num.length; i++) {
        for (int x = 1; x < num.length - i; x++) {
            if (num[x - 1] > num[x]) {
                int temp = num[x - 1];
                num[x - 1] = num[x];
                num[x] = temp;
    
            }
        }
      }
    }
    

提交回复
热议问题