直接插入排序(Java實現)

static void insertionSort(int[] unsorted){
    for (int i = 1; i < unsorted.length; i++) {
        if (unsorted[i - 1] > unsorted[i]) {
            int temp = unsorted[i];
            int j;
            for (j = i - 1; j >= 0 && unsorted[j] > temp; j--) {
                unsorted[j + 1] = unsorted[j];
            }
            unsorted[j + 1] = temp;
        }
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章