-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
52 lines (44 loc) · 929 Bytes
/
shell.c
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
#include <stdio.h>
#define MAX_NUMBER 11
void shell_sort(int*, int);
int main(int argc, char *argv[])
{
int i, length;
int a[MAX_NUMBER] = {3, 11, 6, 4, 9, 5, 7, 8, 10, 2, 1};
length = sizeof(a) / sizeof(int);
printf ("length of the array is %d\n", length);
printf ("Array before Shell sort\n");
for (i = 0; i < length; i++)
{
printf ("%d ", a[i]);
}
printf ("\n");
shell_sort(a, length);
printf ("After sorted.\n");
for (i = 0; i < length; ++i)
{
printf ("%d ", a[i]);
}
return 0;
}
void shell_sort(int* a, int n)
{
int i, j, k, h, v;
int cols[16] = {1291276, 463792, 198768, 86961, 33936,
12776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1};
for (k = 0; k < 16; k++)
{
h = cols[k] ;
for (i = h; i < n; i++)
{
v = a[i];
j = i;
while (j >= h && a[j-h] > v)
{
a[j] = a[j-h];
j = j-h;
}
a[j] = v;
}
}
}