希尔排序算法(java希尔排序算法)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_37657245/article/details/88788621

希尔排序是特殊的插入排序算法, 按照百度百科的定义为:

希尔排序(Shell's Sort)是插入排序的一种又称“缩小增量排序”(Diminishing Increment Sort),是直接插入排序算法的一种更高效的改进版本。希尔排序是非稳定排序算法。该方法因D.L.Shell于1959年提出而得名。

基本思想:先取一个小于n的整数d1作为第一个增量,把文件的全部记录分组。所有距离为d1的倍数的记录放在同一个组中。先在各组内进行直接插入排序;然后,取第二个增量d2<d1重复上述的分组和排序,直至所取的增量

  

=1(

  

<

  

…<d2<d1),即所有记录放在同一组中进行直接插入排序为止。

个人对希尔排序的实现代码java

/**
 * 类Selection.java的实现描述:希尔排序(插入排序的优化版本)
 * 
 * @author wangyangchao 2019年3月23日 下午1:11:20
 */
public class SellInsertationSort extends AbstractArithmeticLogicTemplate {

    @Override
    public void sort(Comparable[] a) {
        long start = System.currentTimeMillis();
        int len = a.length;
        int step = len;
        for (;;) {
            step = step / 2;
            for (int i = 0; i < len; i += step) {
                for (int j = i; j > 0 && less(a[j], a[j - step]); j -= step) {
                    this.exch(a, j, j - step);
                }
            }
            if (step == 1) {
                break;
            }
        }
        System.out.println("cost" + (System.currentTimeMillis() - start));
    }

    public static void main(String[] args) {
        SellInsertationSort shellInsertSort = new SellInsertationSort();
        Comparable[] a = new Integer[10000];
        for (int i = 0; i < 10000; i++) {
            a[i] = new Random().nextInt(1000000);
        }

        //        shellInsertSort.show(a);
        shellInsertSort.sort(a);
        //        shellInsertSort.show(a);
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_37657245/article/details/88788621