package com.insert; import java.util.Arrays; /** * 希尔排序 * * @author wanjn * */ public class ShellSort { public static void main(String[] args) { int[] a = { 50, 3, 6, 1, 7, 9, 6, 2, 23, 5, 7, 234, 34, 6, 2, 7, 99, 65, 3, 5, 675, 23 }; int[] b = { 9, 5, 1 }; System.out.println(Arrays.toString(shellInsertSort(a, b))); ; } private static int[] shellInsertSort(int[] a, int[] b) { int temp; int j; int addIndex; //可以和直接插入排序代码类比,基本上相同,只是比较一次移动的范围是一个增量序列 for (int k = 0; k < b.length; k++) { //增量序列 addIndex = b[k]; for (int i = addIndex; i < a.length; i++) { temp = a[i]; for (j = i - addIndex; j >= 0 && temp < a[j]; j-=addIndex) { a[j + addIndex] = a[j]; } a[j + addIndex] = temp; } } return a; } }