shell排序算法严格来说是基于插入排序的思想,又称为希尔排序,或缩小增量排序:
- 将 又n个元素的数组分成n/2个数字序列,第一个数据 和n/2+1个数据为一对
- 每一次循环使每一个序列排顺序
- 不然,再变成n/4个序列,再次排序
- 不断重复上述过程,随着 序列的减少最后变成一个,就完成了整个排序
package shell排序;
public class Shell {
static final int SIZE = 10;
static void shellSort(int[] a) {
int i, j, h;
int t, temp;
int x = 0;
for (t = a.length / 2; t >= 1; t/=2) {
for (i = t; i < a.length; i++) {
temp = a[i];
j = i - t;
while (j >= 0 && temp < a[j]) {
a[j + t] = a[j];
j -= t;
}
a[j + t] = temp;
}
x++;
System.out.print("第" + x + "步排序 结果是:");
for (h = 0; h < a.length; h++) {
System.out.print(" " + a[h]);
}
System.out.print("\n");
}
}
public static void main(String[] args) {
int[] shuzu = new int[SIZE];
int i;
for (i = 0; i < SIZE; i++) {
shuzu[i] = (int) (100 + Math.random() * (100 + 1));// 初始化数组
}
System.out.print("排序前的数组为:\n");
for (i = 0; i < SIZE; i++) {
System.out.print(shuzu[i] + " ");
}
System.out.print("\n");
shellSort(shuzu);
System.out.print("排序后的数组为:\n");
for (i = 0; i < SIZE; i++) {
System.out.print(shuzu[i] + " ");
}
System.out.print("\n");
}
}