希尔排序

package com.itheima;

/**
* 希尔排序(也是插入排序的一种)
*
* @author Administrator
*
*/
public class ShellSort {

public static void shellSort() {
    int a[] = { 1, 54, 6, 3, 78, 34, 12, 45, 56, 100 };

    double d1 = a.length;

    int temp = 0;

    while (true) {

        d1 = Math.ceil(d1 / 2);

        int d = (int) d1;

        for (int x = 0; x < d; x++) {

            for (int i = x + d; i < a.length; i += d) {

                int j = i - d;

                temp = a[i];

                for (; j >= 0 && temp < a[j]; j -= d) {

                    a[j + d] = a[j];

                }
                //跳出循环 j == -d temp永远存放的是临时的小的变量 把最小的赋给第一个数
                a[j + d] = temp;
            }

        }

        if (d == 1) {

            break;
        }
    }

    for (int i = 0; i < a.length; i++) {

        System.out.println(a[i]);

    }

}

public static void main(String[] args) {

    shellSort();

}

}

你可能感兴趣的:(希尔排序)