shell排序

shell排序是对插入排序的一个改装,它每次排序把序列的元素按照某个增量分成几个子序列,对这几个子序列进行插入排序,
然后不断的缩小增量扩大每个子序列的元素数量,直到增量为一的时候子序列就和原先的待排列序列一样了,此时只需要做少
量的比较和移动就可以完成对序列的排序了。

Best:n Average: nlong^2n or n^(3/2) Worst: Depends on gap sequence; best know is nlong^2n
Memory:1 Stable:No

 // shell排序
void ShellSort(int array[], int length)
{
    int temp;
    // 增量从数组长度的一半开始,每次减小一倍
    for (int increment = length / 2; increment > 0; increment /= 2)
        for (int i = increment; i < length; ++i)
        {
                       int j;
            temp = array[i];
            // 对一组增量为increment的元素进行插入排序
            for (j = i; j >= increment; j -= increment)
            {
                // 把i之前大于array[i]的数据向后移动
                if (temp < array[j - increment])
                {
                    array[j] = array[j - increment];
                }
                else
                {
                    break;
                }
            }
            // 在合适位置安放当前元素
            array[j] = temp;
        }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值