-
Notifications
You must be signed in to change notification settings - Fork 0
/
info21_2.c
94 lines (79 loc) · 1.81 KB
/
info21_2.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
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
// 筑波大学院過去入試問題平成21年2月
#include <stdio.h>
int array_size(int array[]);
void print_array(int a[], int n);
void insertion_sort(int a[], int n);
void shift(int a[], int first, int last);
void insertion_sort_d(int a[], int n, int d);
void shell_sort(int a[], int n);
void shift_d(int a[], int first, int last, int d);
int array1[] = {80, 35, 15, 40, 65};
int dim[] = {1, 2, 4};
int main() {
printf("\n");
printf("<ソート前>\n");
print_array(array1, 5);
printf("\n");
shell_sort(array1, 5);
printf("\n");
printf("<ソート後>\n");
print_array(array1, 5);
printf("\n");
}
void print_array(int a[], int n) {
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
void insertion_sort(int a[], int n) {
int w, j, i;
if (n <= 1)
return;
for (i = 1; i < n; i++) {
printf("i == %d: ", i);
print_array(a, n);
w = a[i];
for (j = 0; j < i && a[j] < a[i]; j++)
continue;
shift(a, j, i);
a[j] = w;
}
printf("i == %d: ", i);
print_array(a, n);
}
void shift(int a[], int first, int last) {
int p;
for (p = last - 1; first <= p; p--) {
a[p + 1] = a[p];
}
}
void insertion_sort_d(int a[], int n, int d) {
int w, i, j, k;
if (n <= d)
return;
for (k = 0; k < n; k = k + 1) {
printf("array1 == ");
print_array(a, n);
for (i = d + k; i < n; i = i + d) {
w = a[i];
for (j = k; j < i && a[j] < a[i]; j = j + d)
continue;
shift_d(a, j, i, d);
a[j] = w;
}
}
}
void shell_sort(int a[], int n) {
int l;
for (l = sizeof(dim) / sizeof(dim[0]) - 1; 0 <= l; l--) {
printf("dim == %d\n", dim[l]);
insertion_sort_d(a, n, dim[l]);
}
}
void shift_d(int a[], int first, int last, int d) {
int q;
for (q = last - d; first <= q; q -= d) {
a[q + d] = a[q];
}
}