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

前端 未结 4 756
野趣味
野趣味 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:45

    Try this :

        for (int i = 0; i < num.length; i++) {
            for (int j = i + 1; j < num.length; j++) {
                if (num[i] > num[j]) {
                    num[i] = num[i] + num[j] - (num[j] = num[i]);
                }
            }
        }
    

提交回复
热议问题