插入排序法

public static void main(String[] args) {
        
        int n[] = { 6, 5, 2, 7, 3, 9, 8 };
        insertSort(n);
        System.out.print("插入排序結果:");
        for (int m : n) {
            System.out.print(m + " ");
        }

    }

    public static void insertSort(int n[]) {
        for (int i = 1; i < n.length; i++) {
            int temp = n[i];
            for (int j = i - 1; j >= 0; j--) {
                if (temp < n[j]) {
                    n[j + 1] = n[j];
                    n[j] = temp;
                } else {
                    break;
                }
            }
        }
    }

 

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