public class ShellSort { public static void main(String args[]) { int a[] = {10,5,10,2}; System.out.println("this is shellsort:"); ShellSort(a); output(a); } public static void output(int a[])//输出 { int i; for(i = 0; i < a.length; i++) { System.out.print(a[i] + " "); } System.out.println(); } public static void ShellSort(int a[])//希尔排序 { int i,j,gap; for(gap = a.length / 2; gap >= 1; gap = gap / 2)//步长 { for(i = gap; i < a.length; i++) { int temp = a[i];//保存a[i]的值 j = i - gap;//从i = 0开始 while(j >= 0 && temp < a[j])//直接插入排序 { a[j + gap] = a[j]; j -= gap; } a[j + gap] = temp; } } } }