package com.thinker.suanfa.sort;

/**
 * @author lzh
 * @apiNote 直接插入法，从小到大排序。
 */
public class DirectSort {

    /**
     * @param arr 需要排序的数组
     */
    private static void directSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int k = i;
            for (int j = i - 1; j >= 0; j--) {
                if (arr[j] > tmp) {
                    arr[j + 1] = arr[j];
                    k = j;
                } else {
                    break;
                }
            }
            arr[k] = tmp;
        }
    }

    public static void main(String[] args) {
        int[] arr = new int[]{4, 2, 6, 8, 9, 3, 1, 5};
        long begin = System.nanoTime();
        directSort(arr);
        long end = System.nanoTime();
        for (int a : arr) {
            System.out.print(a + "\t");
        }
        System.out.println("\nrun time:" + (double) (end - begin) / 1000 + " ms");
    }
}
