天天看點

倒序插入排序算法倒序插入排序算法

倒序插入排序算法

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

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

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

            System.out.println("第"+i+"步排序結果:");  // 輸出每步排序結果
            for(h=;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 = ; i < SIZE; i++) {
            shuzu[i] = (int)( + Math.random()*(+));
        }
        //
        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]);
//        }

    }