public class ShellSort {
public static void sort(int[] array) {
if (null == array||array.length<2){
return;
}
sort(array, 0, array.length - 1);
}
private static void sort(int[] array, int start, int end) {
for (int step = initialStep(end - start + 1); step >= 1; step = (step + 1) / 2 - 1) {
for (int groupIndex = 0; groupIndex < step; groupIndex++) {
insertSort(array, groupIndex, step, end);
}
}
}
private static int initialStep(int length) {
int step = 1;
while ((step + 1) * 2 - 1 < length - 1) {
step = (step + 1) * 2 - 1;
}
return step;
}
private static void insertSort(int[] array, int groupIndex, int step, int end) {
int endIndex = groupIndex;
while ((endIndex + step) <= end) {
endIndex += step;
}
for (int i = groupIndex + step; i <= end; i += step) {
for (int j = groupIndex; j < i; j += step) {
int insertedElem = array[i];
if (array[j] > insertedElem) {
for (int k = i - step; k >= j; k -= step) {
array[k + step] = array[k];
}
array[j] = insertedElem;
break;
}
}
}
}
}
JAVA希尔排序(SHELL-SORT)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前面一口气写了冒泡、选择、插入三个排序算法,感觉今天和他们死磕上了。。。就不该十一点多还看了几眼。。。然后又掉坑里...
- 一、希尔排序思想 希尔排序是基于插入排序的快速的排序算法,先分组后对每组进行直接插入排序,再分组再直接执行插入排序...
- 希尔排序是1959年由 D.L.Shell 提出来的,相对直接排序有较大的改进。希尔排序又叫缩小增量排序。 基本思...
- 前言 希尔排序算法其本质就是插入排序,是直接插入排序算法的一种改进,因 D.L shell 于 1959 年提出而...
- 声明:算法和数据结构的文章均是作者从github上翻译过来,为方便大家阅读。如果英语阅读能力强的朋友,可以直接到s...