算法——希尔排序
原理:按步长进行直接插入排序。适用于短小、基本有序的序列进行排序。
是不稳定的排序。时间复杂度 小于o(n^2)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename T>
void shellSort(vector<T>& v){
for(int step = v.size()/2; step >= 1; step /= 2){
for(int i = step; i < v.size(); ++i){
if(v[i] < v[i-step]){
int j = i;
while(j >= step && v[j] < v[j-step]){
swap(v[j],v[j-step]);
j -= step;
}
}
}
}
}
int main(){
std::vector<int> v {1,3,2,7,5,9,6};
shellSort(v);
for(auto x : v){
cout << x << " ";
}
cout << endl;
return 0;
}