JAVA常见算法题(三十一)---冒泡排序
生活随笔
收集整理的這篇文章主要介紹了
JAVA常见算法题(三十一)---冒泡排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.jege.spring.boot.hello.world;/*** java算法之冒泡排序<br>* 將數組按照從大到小的順序排列<br>* * * @author Administrator**/
public class BubbleSort{public static void main(String[] args){int score[] = {67, 69, 75, 87, 89, 90, 99, 100};for (int i = 0; i < score.length -1; i++){ //最多做n-1趟排序for(int j = 0 ;j < score.length - i - 1; j++){ //對當前無序區間score[0......length-i-1]進行排序(j的范圍很關鍵,這個范圍是在逐步縮小的)if(score[j] < score[j + 1]){ //把小的值交換到后面int temp = score[j];score[j] = score[j + 1];score[j + 1] = temp;}} System.out.print("第" + (i + 1) + "次排序結果:");for(int a = 0; a < score.length; a++){System.out.print(score[a] + "\t");}System.out.println("");}System.out.print("最終排序結果:");for(int a = 0; a < score.length; a++){System.out.print(score[a] + "\t");}}
}
?
運行結果:
?
轉載于:https://www.cnblogs.com/mr-wuxiansheng/p/7136365.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的JAVA常见算法题(三十一)---冒泡排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序5
- 下一篇: CA认证技术的研究与设计