算法 - 希尔排序(Shell Sort)

  • 1959年由唐纳德·希尔(Donald Shell)提出
  • 希尔排序把序列看作是一个矩阵,分成m列,逐列进行排序
  1. m从某个整数逐渐减为1
  2. 当m为1时,整个序列将完全有序
  • 因此,希尔排序也被称为逐渐增量排序(Diminashing Increment Sort)
  • 矩阵的列数取决于步长序列(step sequence)
  1. 比如,如果步长序列为{1,5,19,41,109,…},就代表依次分层109列、41列、19列、5列、1列进行排序
  2. 不同的步长序列,执行效率也不同

实例

  • 希尔本人给出的步长序列是n/2^k,比如n为16时,步长序列是{1,2,4,8}
    在这里插入图片描述
  • 分成8列进行排序
    在这里插入图片描述
  • 分成4列进行排序
    在这里插入图片描述
  • 分成2列进行排序
    在这里插入图片描述
  • 分成1列进行排序
    在这里插入图片描述
  • 不难看出来,从8列变为1列的过程中,逆序对的数量在逐渐减少
  • 因此希尔排序底层一般使用插入排序对每一列进行排序,也有很多资料认为希尔排序是插入排序的改进版

  • 假设有11个元素,步长序列是{1,2,5}
    在这里插入图片描述
  • 假设元素在第col列、第row行、步长(总列数)是step
  • 那么这个元素的数组中的索引是 co l+ row * step
  • 比如9在排序前是第2列、第0行、那么它排序前的索引是 2 + 0 * 5 = 2
  • 比如4在排序前是第2列、第1行、那么它排序前的索引是 2 + 1 * 5 = 7

实现

List<Integer> stepSequence = sedgewickStepSequence();
for (Integer step : stepSequence) {
    sort(step);
}
private void sort(int step) {
    for (int col = 0; col < step; col++) {
        for (int begin = col + step; begin < array.length; begin += step) {
            int cur = begin;
            while (cur > col && cmp(cur, cur - step) < 0) {
                swap(cur, cur - step);
                cur -= step;
            }
        }
    }
}
  • 最好情况的步长序列只有1,且序列几乎有序,时间复杂度为0(n)
  • 空间复杂度为0(1),属于不稳定排序

步长序列

  • 希尔本人给出的步长序列,最坏情况时间复杂度是O(n²)
private List<Integer> shellStepSequence() {
    List<Integer> stepSequence = new ArrayList<>();
    int step = array.length;
    while ((step >>= 1) > 0) {
        stepSequence.add(step);
    }
    return stepSequence;
}
  • 目前已知的最好的步长序列,最坏情况时间复杂度是O(n^(4/3)),1986年由Robert Sedgewick提出
    在这里插入图片描述
private List<Integer> sedgewickStepSequence() {
    List<Integer> stepSequence = new LinkedList<>();
    int k = 0, step = 0;
    while(true) {
        if (k % 2 == 0) {
            int pow = (int) Math.pow(2, k >> 1);
            step = 1 + 9 * (pow * pow - pow);
        } else {
            int pow1 = (int) Math.pow(2, (k - 1) >> 1);
           int pow2 = (int) Math.pow(2, (k + 1) >> 1);
           step = 1 + 8 * pow1 * pow2 - 6 * pow2;
       }
       if (step >= array.length) break;
       stepSequence.add(0, step);
       k++;
    }
    return stepSequence;
}

最终版本

public List<Integer> sortArray(int[] nums) {
    List<Integer> stepSequence = shellStepSequence(nums.length);
    for (Integer step : stepSequence) {
        sort(step, nums);
    }
    List<Integer> array = new ArrayList<>();
    for (int i = 0; i < nums.length; i++) {
        array.add(nums[i]);
    }
    return array;
}
private void sort(int step, int[] nums) {
     // col : 第几列,column简称
    for (int col = 0; col < step; col++) { // 对第col列进行排序
        // col、col+step、col+2*step、col+3*step
        for (int begin = col + step; begin < nums.length; begin += step) {
             int cur = begin;
             int v = nums[cur];
             while (cur > col && v < nums[cur - step]) {
                  nums[cur] = nums[cur - step];
                  cur -= step;
             }
             nums[cur] = v;
        }
    }
}
private List<Integer> shellStepSequence(int length) {
    List<Integer> stepSequence = new ArrayList<>();
   int step = length;
   while ((step >>= 1) > 0) {
       stepSequence.add(step);
   }
   return stepSequence;
}

最终简洁版

public int[] sortArray(int[] nums) {
    for (int step = nums.length >> 1; step >= 1; step >>= 1) {
        for (int col = 0; col < step; col++) { // 对第col列进行排序
            // col、col+step、col+2*step、col+3*step
            for (int begin = col + step; begin < nums.length; begin += step) {
                int cur = begin;
                int v = nums[cur];
                while (cur > col && v < nums[cur - step]) {
                     nums[cur] = nums[cur - step];
                     cur -= step;
                }
                nums[cur] = v;
            }
        }
    }    
    return nums;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值