回答編集履歴

1

全コード追加

2016/07/06 14:02

投稿

ttyp03
ttyp03

スコア16998

test CHANGED
@@ -31,3 +31,237 @@
31
31
 
32
32
 
33
33
  ```
34
+
35
+
36
+
37
+ ---
38
+
39
+ 修正版全コード
40
+
41
+ ```c
42
+
43
+ #include <stdlib.h>
44
+
45
+ #include <time.h>
46
+
47
+ #include <sys/time.h>
48
+
49
+
50
+
51
+ #define VALUE_SIZE (1 * 1024 * 1024)
52
+
53
+
54
+
55
+ struct array_data {
56
+
57
+ int value[VALUE_SIZE];
58
+
59
+ };
60
+
61
+
62
+
63
+ struct list_data {
64
+
65
+ struct list_data* next;
66
+
67
+ int value[VALUE_SIZE];
68
+
69
+ };
70
+
71
+
72
+
73
+ void dump_array(struct array_data* data, int num) {
74
+
75
+ int i;
76
+
77
+
78
+
79
+ for (i = 0; i < num; ++i) {
80
+
81
+ printf("%d\n", data[i].value[0]);
82
+
83
+ }
84
+
85
+ }
86
+
87
+
88
+
89
+ void swap_array(struct array_data* array, int n) {
90
+
91
+ struct array_data tmp;
92
+
93
+
94
+
95
+ tmp = array[n];
96
+
97
+ array[n] = array[n + 1];
98
+
99
+ array[n + 1] = tmp;
100
+
101
+ }
102
+
103
+
104
+
105
+ void eval_array(int num) {
106
+
107
+ struct array_data *array;
108
+
109
+ int n, i;
110
+
111
+
112
+
113
+ // init
114
+
115
+ if ((array = malloc(sizeof(struct array_data) * num)) == NULL) {
116
+
117
+ perror("malloc");
118
+
119
+ exit(1);
120
+
121
+ }
122
+
123
+
124
+
125
+ for (i = 0; i < num; ++i) {
126
+
127
+ array[i].value[0] = i;
128
+
129
+ }
130
+
131
+ //dump_array(array, num);
132
+
133
+
134
+
135
+ // swap
136
+
137
+ long cnt = 0;
138
+
139
+ clock_t et = clock() + 5000000;
140
+
141
+ while(et >= clock()){
142
+
143
+ n = (rand() % (num - 2)) + 1;
144
+
145
+ swap_array(array, n);
146
+
147
+ cnt++;
148
+
149
+ }
150
+
151
+ printf("cnt=%ld\n",cnt);
152
+
153
+ dump_array(array, num);
154
+
155
+
156
+
157
+ free(array);
158
+
159
+ }
160
+
161
+
162
+
163
+ void dump_list(struct list_data* head) {
164
+
165
+ struct list_data *ld;
166
+
167
+
168
+
169
+ ld = head;
170
+
171
+ while (ld != NULL) {
172
+
173
+ printf("%d\n", ld->value[0]);
174
+
175
+ ld = ld->next;
176
+
177
+ }
178
+
179
+ }
180
+
181
+
182
+
183
+ void swap_list(struct list_data* head, int n) {
184
+
185
+
186
+
187
+ }
188
+
189
+
190
+
191
+
192
+
193
+
194
+
195
+ void eval_list(int num) {
196
+
197
+ struct list_data *head, *ld;
198
+
199
+ int n, i;
200
+
201
+
202
+
203
+ // init
204
+
205
+ if ((head = malloc(sizeof(struct list_data) * num)) == NULL) {
206
+
207
+ perror("malloc");
208
+
209
+ exit(1);
210
+
211
+ }
212
+
213
+
214
+
215
+ ld = head;
216
+
217
+ for (i = 0; i < num - 1; ++i) {
218
+
219
+ ld->next = ld + 1;
220
+
221
+ ld->value[0] = i;
222
+
223
+
224
+
225
+ ld = ld->next;
226
+
227
+ }
228
+
229
+ ld->next = NULL;
230
+
231
+ ld->value[0] = num - 1;
232
+
233
+ //dump_list(head);
234
+
235
+
236
+
237
+ // swap
238
+
239
+ n = (rand() % (num - 2)) + 1;
240
+
241
+ swap_list(head, n);
242
+
243
+ dump_list(head);
244
+
245
+
246
+
247
+ free(head);
248
+
249
+ }
250
+
251
+
252
+
253
+ int main(int argc, char *argv[]) {
254
+
255
+ eval_array(10);
256
+
257
+ //eval_list(10);
258
+
259
+
260
+
261
+ exit(0);
262
+
263
+ }
264
+
265
+
266
+
267
+ ```