Shell Sort

学习目标:

Shell Sort

学习时间:

2021/7/28 20:00 ~ 21:30

学习内容:

1.what

the algorithm breaks an original set into smaller subsets and then each of those is sorted using Insertion sort.

2.how
  • how it makes the subsets is not straightforward.
  • It doesn’t choose neighboring elements to form a subset as we might expect.
  • Rather, shell sort uses the so-called interval or gap for subset creation.
3.example

if we have the gap I, it means that one subset will contain the elements that are I positions apart.

4.process
  • Firstly, the algorithm sorts the elements that are far away from each other.
  • Then, the gap becomes smaller and closer elements are compared.
  • This way, some elements that aren’t in a correct position can be positioned faster than
  • if we made the subsets out of the neighboring elements.

学习产出:

  • We first created a gap sequence with a for loop
  • then did the insertion sort for each gap size
public void sort(int arrayToSort[]) {
    int n = arrayToSort.length;

    for (int gap = n / 2; gap > 0; gap /= 2) {
        for (int i = gap; i < n; i++) {
            int key = arrayToSort[i];
            int j = i;
            while (j >= gap && arrayToSort[j - gap] > key) {
                arrayToSort[j] = arrayToSort[j - gap];
                j -= gap;
            }
            arrayToSort[j] = key;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值