直接插入排序的小改进——希尔排序
希尔排序算法思想:
- 在排序的过程中,整个排序区间被分为几个子表。
- 对每个子表分别进行直接插入排序。
- 由于\(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