//shellSort.h
#ifndef _SHELLSORT_H_
#define _SHELLSORT_H_
class shellSort
{
public:
shellSort();
void sort(int *,int,int);
int a[200000];
};
#endif
//shellSort.cpp
#include "shellSort.h"
#include <iostream>
#include <stdlib.h>
using namespace std;
shellSort::shellSort()
{
for(int i = 0; i < 200000; ++i)
{
srand(i);
this->a[i] = rand() * 10;
}
}
void shellSort::sort(int * a,int l,int r)
{
int h;
for(h = 0; h <= (r - l) / 9; h = 3 * h + 1);
for(;h > 0; h /= 3)
{
for(int i = l + h; i < r; i++)
{
int j = i, v = a[i];
while(j >= l + h && v < a[j-h])
{
a[j] = a[j - h]; j -= h;
}
a[j] = v;
}
}
}
//main.cpp
#include "shellSort.h"
#include <iostream>
using namespace std;
int main(int argc,char* argv[])
{
shellSort shell;
shell.sort(shell.a,0,200000);
for(int i = 0 ; i < 200000; ++i)
{
cout << shell.a[i] << endl;
}
while(cin.get());
return 0;
}
步长1 4 13 40 ... (3*h+1) 说是这个步长好。