排序算法@c++描述-shell排序

2.shell排序

#include 
#include 

using namespace std;

template 
void shellSort(vector &a)
{
    for (int gap = a.size() / 2; gap > 0; gap /= 2) {
        for (int i = gap; i < a.size(); i++) {
            T tmp = a[i];
            int j = i;
            for (; j >= gap && tmp < a[j - gap]; j -= gap)
                a[j] = a[j - gap];
            a[j] = tmp;
        }
    }
}

int main()
{
    vector test = {190, 435, 834, 8954, 923, 56, 20 ,1, 934};
    shellSort(test);
    for (auto i : test)
        cout << i << " ";
    cout << endl;
    return 0;
}
  • 运行结果:
$ ./a.out
1 20 56 190 435 834 923 934 8954

  • 我的个人主页:http://www.techping.cn/
  • 我的个人站点博客:http://blog.techping.cn/
  • 我的CSDN博客:http://blog.csdn.net/techping
  • 我的:http://www.jianshu.com/users/b2a36e431d5e/timeline
  • 我的GitHub:https://github.com/techping

你可能感兴趣的:(排序算法@c++描述-shell排序)