什么是希尔排序:https://baike.baidu.com/item/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F/3229428

解析:

public class ShellSort{
    public void shellSort(int[] array,int n){
        int i,j,gap;
        int temp;
        for(gap = n/2;gap > 0;gap /= 2){
            for(i = gap;i<n;i++){
                for(j = i-gap;j >= 0 && array[j] > array[j+gap];j -= gap){
                    temp = array[j];
                    array[j] = array[j+gap];
                    array[j+gap] = temp;
                }
                //打印每趟排序结果
                for(int m = 0;m <= array.length - 1;m++){
                    System.out.print(array[m] + "\t");
                }
                System.out.println();
            }
        }
    }

    public static void main(String[] args){
        ShellSort shellSort = new ShellSort();
        int[] array = {5,69,12,3,56,789,2,5648,23};
        shellSort.shellSort(array,array.length);
        for(int m = 0;m <= array.length - 1;m++){
            System.out.print(array[m] + "\t");
        }
    }
}

 

最后修改于 2020-04-10 16:47:13
如果觉得我的文章对你有用,请随意赞赏
扫一扫支付
上一篇