1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
void ssort(vector<int> &list)
{
size_t size = list.size();
int temp;
size_t i, j, dx=0;
size_t harr[] =
{
0, /*terminal condition for sort */
1,
10,
53,
105,
542,
1047,
6239,
16256,
56720,
134096,
579823,
1000000,
5430201,
999999999 /*terminal condition for find index */
};
while(size/2 > harr[++dx]); /*get index in sequence, +1 */
for(dx--;dx;dx--) /*while not 0 in sequence*/
{
const size_t hdx = harr[dx]; /*saves some access time! C ok?! */
for( i = hdx, temp = list[i]; i< size; temp = list[++i])
{
for(j = i; (j >= hdx) && (list[j-hdx] > temp);
list[j] = list[j-hdx], j -= hdx);
list[j] = temp;
}
}
}
int main()
{
vector<int> v(1000000);
std::default_random_engine generator;
std::uniform_int_distribution<int> distribution(-10000,10000);
for (int i=0; i<v.size(); ++i)
{
v[i] = distribution(generator);
}
auto start = chrono::high_resolution_clock::now();
ssort(v);
auto end = chrono::high_resolution_clock::now();
cout << 1.0e-6*(chrono::duration_cast<chrono::microseconds>(end - start).count()) << " seconds\n";
}
|