Shell排序

#include <iostream> #include <vector> #include <windows.h> using std::vector; template<class T> void shell_sort(vector<T> &v) //Shell sort { const int n = v.size(); for(int gap=n/2; 0<gap; gap/=2) for(int i=gap; i<n; ++i) for(int j=i-gap; 0<=j; j-=gap) if(v[j+gap]<v[j]) {//change T temp = v[j]; v[j] = v[j+gap]; v[j+gap] = temp; } } template<class T> void print(vector<T> &v) { const size_t n = v.size(); for(unsigned i=0; i<n; ++i) std::cout << v[i] << ','; } int main() { vector<int> v; srand((unsigned) ::GetTickCount()); for(int i=0; i<100; ++i) { int r = rand()%1000; v.push_back(r); } print(v); shell_sort(v); std::cout << '/n'; print(v); return 0; }  

你可能感兴趣的:(shell,include)