直接插入排序的小改进——希尔排序

转载请注明出处,谢谢!

希尔排序算法思想:

  • 在排序的过程中,整个排序区间被分为几个子表。
  • 对每个子表分别进行直接插入排序。
  • 由于\(N^2>N^2_1+N^2_2+N^2_3+ ... +N^2_n,(N=N_1+N_2+N_3+ ... +N_n)\)
    所以对每个子表排序所耗费的时间之和要小于对整个区间排序所耗费的时间。
  • 通过对子表小范围的排序,将排序区间调整成基本有序的序列,
  • 不断减少子表的个数(即扩大子表的长度), 直至子表的个数为1, 完成整个排序操作。

插入排序

  • 直接插入排序
  • 折半插入排序
  • 二路插入排序
  • 希尔排序

算法说明

  • 希尔排序也可以用于大规模数组,并不怎么关心数组初始的状态。
  • 希尔排序的运行时间小于平方级。
  • 不同的增量对算法运行时间改变较小,大多仅有学术研究价值。

ArrayList实现:

import java.util.ArrayList;
import java.util.Random;

public class Shell {

    
    public static void sort(ArrayList<Integer> al){
        int N = al.size();
        int h =1;
        while(h<N/3){
            h=h*3+1;
        }
        while(h>0){
            for (int i = h; i < N; i++) {
                for (int j = i; j >= h && al.get(j)<al.get(j-h); j-=h) {
                    Integer tempInt = al.get(j);
                    al.set(j, al.get(j-h));
                    al.set(j-h, tempInt);
                }
                System.out.println("h: "+h+" \t"+al);
            }
            h=h/3;
        }
    }
    
    public static void main(String[] args) {
        int n = 16;
        ArrayList<Integer> al = new ArrayList<>(n);
        // 创建一个随机数生成器
        Random rand = new Random();
        // 添加1-100的随机整数
        
        for (int i = 0; i < n; i++) {
            al.add(new Integer(Math.abs(rand.nextInt(100))+1));
        }
        System.out.println("The ArrayList Sort Before:\n   \t" + al+"\nsorting:");
        Shell.sort(al);

    }

}

Output:

The ArrayList Sort Before:
    [48, 58, 76, 16, 23, 86, 91, 82, 35, 2, 12, 61, 84, 43, 69, 86]
sorting:
h: 13   [43, 58, 76, 16, 23, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 13   [43, 58, 76, 16, 23, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 13   [43, 58, 76, 16, 23, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 4    [23, 58, 76, 16, 43, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 4    [23, 58, 76, 16, 43, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 4    [23, 58, 76, 16, 43, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 4    [23, 58, 76, 16, 43, 86, 91, 82, 35, 2, 12, 61, 84, 48, 69, 86]
h: 4    [23, 58, 76, 16, 35, 86, 91, 82, 43, 2, 12, 61, 84, 48, 69, 86]
h: 4    [23, 2, 76, 16, 35, 58, 91, 82, 43, 86, 12, 61, 84, 48, 69, 86]
h: 4    [23, 2, 12, 16, 35, 58, 76, 82, 43, 86, 91, 61, 84, 48, 69, 86]
h: 4    [23, 2, 12, 16, 35, 58, 76, 61, 43, 86, 91, 82, 84, 48, 69, 86]
h: 4    [23, 2, 12, 16, 35, 58, 76, 61, 43, 86, 91, 82, 84, 48, 69, 86]
h: 4    [23, 2, 12, 16, 35, 48, 76, 61, 43, 58, 91, 82, 84, 86, 69, 86]
h: 4    [23, 2, 12, 16, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 4    [23, 2, 12, 16, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 23, 12, 16, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 23, 16, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 48, 69, 61, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 48, 61, 69, 43, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 61, 69, 58, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 91, 86]
h: 1    [2, 12, 16, 23, 35, 43, 48, 58, 61, 69, 76, 82, 84, 86, 86, 91]

——@guoyangde http://www.cnblogs.com/LittleTreasureBox/p/8904016.html

转载于:https://www.cnblogs.com/LittleTreasureBox/p/8908055.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值