Skip to content

Commit 630a420

Browse files
shell sort
1 parent fa474f1 commit 630a420

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

shell_sort.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <cstdio>
2+
3+
#define ELEMENT_COUNT 100000
4+
5+
using namespace std;
6+
7+
int n, d[ELEMENT_COUNT];
8+
9+
void Shell_sort()
10+
{
11+
for (int gc = n >> 1; gc >= 1; gc >>= 1)
12+
{
13+
for (int s = 0; s < gc; s++)
14+
{
15+
for (int i = s; i < n; i += gc)
16+
{
17+
for (int j = i - gc; j >= 0 && d[j] > d[j + gc]; j -= gc)
18+
{
19+
int temp = d[j];
20+
d[j] = d[j + gc];
21+
d[j + gc] = temp;
22+
}
23+
}
24+
}
25+
}
26+
}
27+
28+
int main()
29+
{
30+
scanf("%d", &n);
31+
for (int i = 0; i < n; i++)
32+
{
33+
scanf("%d", &d[i]);
34+
}
35+
Shell_sort();
36+
for (int i = 0; i < n; i++)
37+
{
38+
printf("%d ", d[i]);
39+
}
40+
return 0;
41+
}
42+
43+

0 commit comments

Comments
 (0)