继续上图和代码
package com.lyj.sort;
public class ShellSort {
/**
* @param args
*/
public static void main(String[] args) {
int[] array = { 76, 81, 60, 22, 98, 33, 12, 1, 19 };
// 排序前
System.out.print("排序前: ");
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
// 排序
shellSort(array);
// 排序后
System.out.print("排序后: ");
for (int i : array) {
System.out.print(i + " ");
}
}
private static void shellSort(int[] array) {
int d = array.length;
while ((d = d / 2) != 0) {
for (int i = 0; i < d; i++) { // 按增量d分组
int h = i;
// 以下相当于插入排序
for (int j = 0; j < (array.length / d) - 1; j++) { // 每组数据的数量
for (int k = h; k >= 0; k--) { // 排序
if (array[k] > array[k + d]) {
int temp = array[k];
array[k] = array[k + d];
array[k + d] = temp;
}
}
h = h + d;
}
}
System.out.print("增量d=" + d + "的排序 ");
for (int in : array) {
System.out.print(in + " ");
}
System.out.println();
}
}
}