java 冒泡排序

class BubbleSort{
    public static void main(String[] args){
        int score[] = {64, 34, 23, 75, 68, 13, 98, 89,54,56};
        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");
        }
    }
}
發佈了74 篇原創文章 · 獲贊 45 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章