/*
* author: cw1997 a10702005 [changwei1006@gmail.com]
* datetime: 2018-10-22 23:29:39
*/
-sharpinclude <iostream>
-sharpinclude <ctime>
-sharpinclude <cmath>
void printarr(long n, long arr[]) {
printf("\n");
for (int i = 0; i < n; PPi) {
printf("%ld,", arr[i]);
}
printf("\n");
}
void swap(long &a, long &b) {
long t = b;
b = a;
a = t;
}
long powlong(long a, long b) {
if (b == 0) {
return 1;
}
long r = a;
for (int i = 1; i < b; PPi) {
r *= a;
}
return r;
}
long calcIncrement(long n) {
long inc = 1;
long gap = 0;
while (gap < n) {
// old = gap;
gap = powlong(2, inc) - 1;
PPinc;
}
return inc-2;
}
void shellsort(long n, long arr[]) {
long inc = calcIncrement(n);
// printf("inc:%ld,", inc);
long gap;
for (long i = inc; i > 0; --i) {
gap = powlong(2, i) - 1;
printf("\ni:%ld,gap:%ld\n", i, gap);
for (long j = 0; j + gap < n; PPj) {
for (long k = j + gap; k + gap < n; k+=gap) {
if (arr[j] > arr[k]) {
swap(arr[j], arr[k]);
}
}
}
printarr(n, arr);
}
}
void insertsort(long n, long arr[]) {
for (int i = 0; i < n; PPi) {
for (int j = i+1; j < n; PPj) {
if (arr[i] > arr[j]) {
swap(arr[i], arr[j]);
}
}
}
}
void f(long n) {
long arr[n];
for (long i = n-1; i >= 0; --i) {
arr[n-i-1] = i+1;
}
printarr(n, arr);
shellsort(n, arr);
// insertsort(n, arr);
printarr(n, arr);
}
void run(long n) {
// long n;
// long result;
clock_t start, stop;
// std::cout << "Please type input size(n): " << n << std::endl;
// std::cin >> n;
start = clock(); //
f(n);
stop = clock(); //
// std::cout << "Fibonacci(10)= " << result << std::endl;
std::cout.setf(std::ios::fixed);
std::cout << "Elapsed time: " << double(stop - start) / CLOCKS_PER_SEC << " sec" << std::endl << std::endl;
}
int main() {
// printf("%ld\n", powlong(2,0));
// printf("%ld\n", powlong(2,1));
// printf("%ld\n", powlong(2,2));
// printf("%ld\n", powlong(2,3));
run(200);
// for (int i = 1; i < 23; PPi) {
// run(i*10000);
// }
return 0;
}
The result of running out is
/Users/changwei/CLionProjects/ShellSort/cmake-build-debug/ShellSort
200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,
i:7,gap:127
200,199,198,197,196,195,194,193,192,191,190,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,137,136,135,134,133,132,131,130,129,128,127,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,74,73,72,71,70,69,68,67,66,65,64,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,
i:6,gap:63
74,73,72,71,70,69,68,67,66,65,64,126,125,124,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,105,104,103,102,101,100,99,98,97,96,95,94,93,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,137,136,135,134,133,132,131,130,129,128,127,189,188,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,168,167,166,165,164,163,162,161,160,159,158,157,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,200,199,198,197,196,195,194,193,192,191,190,63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,
i:5,gap:31
45,44,43,42,41,40,39,38,37,36,35,34,33,32,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,74,73,72,71,70,69,68,67,66,65,64,95,94,63,92,91,90,89,88,87,86,85,84,83,82,81,80,79,78,77,76,75,105,104,103,102,101,100,99,98,97,96,126,125,93,123,122,121,120,119,118,117,116,115,114,113,112,111,110,109,108,107,106,137,136,135,134,133,132,131,130,129,128,127,158,124,156,155,154,153,152,151,150,149,148,147,146,145,144,143,142,141,140,139,138,168,167,166,165,164,163,162,161,160,159,189,157,187,186,185,184,183,182,181,180,179,178,177,176,175,174,173,172,171,170,169,200,199,198,197,196,195,194,193,192,191,190,188,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,
i:4,gap:15
20,19,18,17,16,30,29,28,27,26,25,24,23,22,21,45,44,43,42,31,40,39,38,37,36,35,34,33,32,47,46,60,59,58,41,56,55,54,53,52,51,50,49,48,62,61,74,73,72,57,70,69,68,67,66,65,64,80,79,63,77,76,75,89,71,87,86,85,84,83,82,81,95,94,78,92,91,90,105,88,103,102,101,100,99,98,97,96,111,110,93,108,107,106,104,119,118,117,116,115,114,113,112,126,125,109,123,122,121,120,136,135,134,133,132,131,130,129,128,127,143,124,141,140,137,138,152,151,150,149,148,147,146,145,144,158,142,156,155,139,153,168,167,166,165,164,163,162,161,160,159,174,157,172,154,170,169,183,182,181,180,179,178,177,176,175,189,173,187,171,185,184,200,199,198,197,196,195,194,193,192,191,190,188,186,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,
i:3,gap:7
11,10,9,8,14,13,12,20,19,18,15,16,23,22,21,27,26,17,24,30,29,28,38,37,25,35,31,33,32,45,44,36,42,34,40,39,47,46,43,52,51,41,49,48,54,53,59,58,50,56,55,62,60,67,66,65,57,70,69,61,74,73,72,64,71,79,63,77,76,75,82,80,87,68,78,84,83,89,81,88,86,85,92,91,90,98,95,94,102,101,93,99,105,97,96,111,110,100,108,107,106,103,112,118,117,109,115,114,104,119,126,125,116,123,122,113,120,129,128,127,133,124,121,130,136,135,134,143,132,131,140,137,138,145,144,150,141,148,147,139,152,151,158,142,156,155,146,153,161,160,149,165,157,163,154,168,167,159,174,164,172,162,170,169,166,175,181,173,179,171,177,176,182,189,180,187,178,185,183,193,192,191,190,188,186,184,200,199,198,197,196,195,194,7,6,5,4,3,2,1,
i:2,gap:3
5,4,6,8,7,9,11,10,13,12,14,16,18,15,17,23,20,19,24,22,21,25,26,29,27,30,31,28,32,37,33,35,39,34,36,41,44,38,42,47,40,43,49,46,45,50,48,54,52,51,55,53,56,57,62,59,58,63,60,61,64,65,67,66,68,72,70,69,76,74,71,78,75,73,79,81,77,80,84,82,86,85,83,89,87,88,91,90,92,95,93,96,101,94,98,103,97,99,105,104,100,108,107,102,111,109,106,114,110,113,117,112,115,120,122,116,121,125,118,123,129,119,126,130,124,127,131,128,132,133,136,135,134,139,137,138,140,142,147,141,144,149,143,146,150,145,148,151,154,152,153,155,157,156,158,160,163,159,162,164,161,166,167,165,168,170,169,173,175,171,174,176,172,177,179,181,178,180,182,184,185,183,189,188,186,190,192,187,193,194,191,195,197,196,198,200,199,3,2,1,
i:1,gap: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,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,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,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,1,
Elapsed time: 0.000718 sec
Process finished with exit code 0