希尔排序

别名:缩小增量排序
实质:插入排序
理论时间复杂度:O(n^2)
空间复杂度:O(1)

代码实现

public class Demo {

    public static void main(String[] args) {
        int arr[] = {5, 3, 9, 7, 6, 1, 2, 4, 8, 0};
        shellSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void shellSort(int[] arr){
        int gap = arr.length;
        while (gap > 1) {
            gap /= 2;
            for (int i=0; i < gap; i++) {
                for (int j=i+gap; j<arr.length; j+=gap){
                    int tmp = arr[j];
                    int k;
                    for(k = j-gap; k>=0 && arr[k]>tmp; k = k-gap){
                        arr[k+gap] = arr[k];
                    }
                    arr[k + gap] = tmp;
                }
            }
        }
    }
}

排序过程

[5, 3, 9, 7, 6, 1, 2, 4, 8, 0]
[1, 3, 9, 7, 6, 5, 2, 4, 8, 0]
[1, 2, 9, 7, 6, 5, 3, 4, 8, 0]
[1, 2, 4, 7, 6, 5, 3, 9, 8, 0]
[1, 2, 4, 7, 6, 5, 3, 9, 8, 0]
[1, 2, 4, 7, 0, 5, 3, 9, 8, 6]
[1, 2, 4, 7, 0, 5, 3, 9, 8, 6]
[0, 2, 1, 7, 4, 5, 3, 9, 8, 6]
[0, 2, 1, 7, 3, 5, 4, 9, 8, 6]
[0, 2, 1, 7, 3, 5, 4, 9, 8, 6]
[0, 2, 1, 7, 3, 5, 4, 9, 8, 6]
[0, 2, 1, 5, 3, 7, 4, 9, 8, 6]
[0, 2, 1, 5, 3, 7, 4, 9, 8, 6]
[0, 2, 1, 5, 3, 6, 4, 7, 8, 9]
[0, 2, 1, 5, 3, 6, 4, 7, 8, 9]
[0, 1, 2, 5, 3, 6, 4, 7, 8, 9]
[0, 1, 2, 5, 3, 6, 4, 7, 8, 9]
[0, 1, 2, 3, 5, 6, 4, 7, 8, 9]
[0, 1, 2, 3, 5, 6, 4, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值