package com.thinker.suanfa.sort;

/**
 * @author lzh
 * @apiNote 希尔排序，从小到大
 */
public class ShellSort {

    private static void shellSort(int[] arr, int step) {
        for (; step > 0; step /= 2) {
            for (int i = step; i < arr.length; i += step) {
                int tmp = arr[i];
                int k = i;
                for (int j = i - step; j >= 0; j -= step) {
                    if (arr[j] > tmp) {
                        arr[j + step] = 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();
        shellSort(arr, arr.length);
        long end = System.nanoTime();
        for (int a : arr) {
            System.out.print(a + "\t");
        }
        System.out.println("\nrun time:" + (double) (end - begin) / 1000 + " ms");
    }
}
