zoukankan      html  css  js  c++  java
  • Shell Sort(希尔排序)

      这个排序算法很厉害,我个人很喜欢这个算法,但算法的时间复杂度难计。算法对增量(这里也称作step(步长))的选择也需要注意,只记得个希尔增量的最坏情况为O(n^2)、Hibbard增量的最坏情况为O(n^3/2)(书上有证明),书上说Hibbard增量的希尔排序平均情形运行时间基于模拟的结果被认为是O(n^5/4),但暂时没人证明出这个结果。

      算法的代码实现有许多种,我就把《数据结构与算法分析》上的代码用C++敲了几遍当做学习过了,顺便学习C++,记不得的时候再回来复习。

      书《数据结构与算法分析》中给出的代码如下(希尔增量):

    #include <iostream>
    #include <vector>
    using namespace std;
    void ShellSort(vector<int>& nums) {
        for(int Increment = (int)nums.size()/2; Increment > 0; Increment /= 2)
            for(int i = Increment; i < (int)nums.size(); i++) {
                int j, temp = nums[i];
                for(j = i; j >= Increment; j -= Increment)
                    if(temp < nums[j - Increment]) nums[j] = nums[j - Increment];
                    else break;
                nums[j] = temp;
            }
    }
    int main() {
        int n;
        cin >> n;
        vector<int> nums(n);
        for(int i = 0; i < (int)nums.size(); i++)
            cin >> nums[i];
        ShellSort(nums);
        for(int i = 0; i < (int)nums.size(); i++)
            cout << nums[i] << ' ';
        return 0;
    }
    

      

      

  • 相关阅读:
    07_Go语言 ( 切片)
    06_Go语言( 数组)
    05_Go语言( 流程控制)
    04_Go语言( 运算符)
    02_Go语言(变量和常量)
    01_Go语言(环境的搭建)
    云电脑直播简单指南
    统信UOS共享打印机配置
    #插头dp#洛谷 5074 Eat the Trees
    #状压dp#洛谷 3959 [NOIP2017 提高组] 宝藏
  • 原文地址:https://www.cnblogs.com/darkchii/p/8178915.html
Copyright © 2011-2022 走看看