-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell_sort.c
53 lines (40 loc) · 1.01 KB
/
shell_sort.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
51
52
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 0x1EFFFF
void initv(int v[], int n);
void prt_arr(int v[], int n);
void shellsort(int v[], int n);
int v[MAX_SIZE];
int main()
{
initv(v, MAX_SIZE);
shellsort(v, MAX_SIZE);
prt_arr(v, MAX_SIZE);
}
void initv(int v[], int n)
{
for (int i = 0; i < n; i++)
v[i] = (int) random();
}
void prt_arr(int v[], int n)
{
for (int i = 0; i < n; i++)
printf("%d\n", v[i]);
}
void shellsort(int v[], int n)
{
int tmp, j, k, gap;
for (gap = n / 2; gap > 0; gap /= 2)
for (j = gap; j < n; j++)
for (k = j - gap; k >= 0 && v[k] > v[k+gap]; k -= gap)
tmp = v[k], v[k] = v[k+gap], v[k+gap] = tmp;
/* for (k = j - gap; k >= 0 && v[k] > v[k+gap]; k -= gap)
tmp = v[k], v[k] = v[k+gap], v[k+gap] = tmp;
*/
/* for (j = gap; j < n; j++) {
int k = j - gap;
if (v[k] > v[k+gap])
tmp = v[k], v[k] = v[k+gap], v[k+gap] = tmp;
}
*/
}