閱讀931 返回首頁    go 阿裏雲 go 技術社區[雲棲]


冒泡排序-JAVA

冒泡排序;
public class MaoPaoSort {

public static void sort(int [] arr){
    if(arr.length > 1){
        int temp;
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {  //無論如何 第一次排序完後 最後一位應該是極值,下次範圍為0 - length-1-i
                if(arr[j] > arr[j+1]){
                    temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
}


public static void main(String[] args) {
    int [] arr = {7,8,4,2,3,1,5,9,6,9};
    sort(arr);
    for (int i : arr) {
        System.out.print(i+" ");
    }
}

}

最後更新:2017-09-02 16:02:45

  上一篇:go  做硬件趟坑血淚史,你中過幾個?不看必入坑
  下一篇:go  快速排序實現-JAVA