First take an integer d1 less than n as the first increment, and divide all records in the file into d1 groups. All records whose distance is a multiple of dl are placed in the same group. First perform direct insertion sorting within each group; then, take the second increment d2
Schematic diagram:
##Source codepackage com.zc.manythread; /** * * @author 偶my耶 * */ public class ShellSort { public static int count = 0; public static void shellSort(int[] data) { // 计算出最大的h值 int h = 1; while (h <= data.length / 3) { h = h * 3 + 1; } while (h > 0) { for (int i = h; i < data.length; i += h) { if (data[i] < data[i - h]) { int tmp = data[i]; int j = i - h; while (j >= 0 && data[j] > tmp) { data[j + h] = data[j]; j -= h; } data[j + h] = tmp; print(data); } } // 计算出下一个h值 h = (h - 1) / 3; } } public static void print(int[] data) { for (int i = 0; i < data.length; i++) { System.out.print(data[i] + "\t"); } System.out.println(); } public static void main(String[] args) { int[] data = new int[] { 4, 3, 6, 2, 1, 9, 5, 8, 7 }; print(data); shellSort(data); print(data); } }