希尔排序

来源:互联网 发布:创盈门窗软件 编辑:程序博客网 时间:2024/06/05 00:14
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;}}}}