冒泡排序-java實現(xiàn)例子


package com.zsh;

public class Test {
    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++){    //對當(dāng)前無序區(qū)間score[0......length-i-1]進(jìn)行排序(j的范圍很關(guān)鍵,這個范圍是在逐步縮小的)
                if(score[j] < score[j + 1]){    //把小的值交換到后面
                    int temp = score[j];
                    score[j] = score[j + 1];
                    score[j + 1] = temp;
                }
            }            
            System.out.print("第" + (i + 1) + "次排序結(jié)果:");
            for(int a = 0; a < score.length; a++){
                System.out.print(score[a] + "\t");
            }
            System.out.println("");
        }
            System.out.print("最終排序結(jié)果:");
            for(int a = 0; a < score.length; a++){
                System.out.print(score[a] + "\t");
       }
    }
}

冒泡排序輸出結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容