一般的希尔排序

/*************************************************************************
	> File Name: shellsort.cpp
	> Author:keson 
	> Mail:[email protected] 
	> Created Time: 2014年11月30日 星期日 10时05分18秒
 ************************************************************************/

#include<iostream>
#include<vector>
#include<fstream>
#include<string>


using namespace std;


template<typename Comparable>
void shellsort(vector<Comparable> &a)
{
    for(int gap=a.size()/2;gap>0;gap/=2)
       for(int i=gap;i<a.size();++i)
    {
        Comparable tmp=std::move(a[i]);
        int j=i;
        for(;j>=gap&&tmp<a[j-gap];j-=gap)
           a[j]=std::move(a[j-gap]);
        a[j]=std::move(tmp);
    }
}


int main()
{

    ofstream out;
    out.open("NUMBER_FILE");
    int SIZE=1000000;

    while(SIZE)
    {
        out<<rand()%10000<<" ";
        SIZE--;
    }
    
    out.close();

    ifstream in;
    in.open("NUMBER_FILE");
    ofstream out2("SORT1");

    vector<int>vec;
    int val;
    while(in>>val)
      vec.push_back(val);
    clock_t start_time=clock();
    shellsort(vec);
    clock_t end_time=clock();
    cout<<"Running time is:"<<
    static_cast<double>(end_time-start_time)/CLOCKS_PER_SEC<<" S"<<endl;
    for(auto c:vec)
      out2<<c<<" ";
    
    in.close();
    out2.close();
}


你可能感兴趣的:(一般的希尔排序)