倒序插入排序算法

倒序插入排序算法

public class p4_8 {  // 倒序插入排序

    static final int SIZE = 10;
    static void insertionSort(int[] a,int len){  // 插入排序
        int i,j,t,h;

        for(i = 1;i<len;i++){
            t=a[i];
            j=i-1;
            while (j>=0 && t>a[j]){   // 返序
                a[j+1] = a[j];
                j--;
            }
            a[j+1] = t;

            System.out.println("第"+i+"步排序結果:");  // 輸出每步排序結果
            for(h=0;h<len;h++){
                System.out.print(" "+a[h]);
            }
            System.out.println();
        }

    }

    public static void main(String[] args) {
        int[] shuzu = new int[SIZE];
        for (int i = 0; i < SIZE; i++) {
            shuzu[i] = (int)(100 + Math.random()*(100+1));
        }
        //
        System.out.println("排序前數組爲:");
        System.out.println("begin time"+ new Date());
//        for (int i = 0; i < shuzu.length; i++) {
//            System.out.print("  "+ shuzu[i]);
//        }
        //
        System.out.println();
        insertionSort(shuzu,SIZE);
        System.out.println("排序後數組爲:");
        System.out.println("end time"+ new Date());
//        for (int i = 0; i < shuzu.length; i++) {
//            System.out.print("  "+ shuzu[i]);
//        }

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