Многопоточная сортировка Шелла с применением процессов
05.10.2013, 01:05. Показов 1357. Ответов 3
Собственно,что я вообще делаю :
сначала разделяю исходный массив на 4 подмассива,далее в главном процессе создаю дочерний(файловые дескрипторы file_pipes1),заходя в дочерний процесс делаю и в нём fork,а потом в главном процессе создаю его второй потомок(файловые дескрипторы file_pipes3).
Cначала каждый подмассив сортируется в одном из 4ёх процессов,затем дочерние процессы передают по каналам свои отсортированные массивы,чтобы родители произвели сортировку слиянием,после этого один из родителей передаёт своему "деду" массив,который он получил после сортировки слиянием,чтобы тот произвёл финальную сортировку слиянием.
Никак не могу решить 2 проблемы :
1) какой-то косяк в самой сортировке : то до конца сортирует,то не до конца
2) проблема при передаче массивов через каналы: читается что-то не то =/
Почти до конца программы наставил принтфов,вот что выводит:
I'm 1st process! after shell sort mass: 1222467888
I'm 4th process! after shell sort mass: 1265566788
I'm 2nd process! after shell sort mass: 2257799999
I'm 3rd process! after shell sort mass: 6112445688
I'm 2nd process! I get this massive from 3rd process: 6 1 1 4 6 4 2 8 5 8
I'm 1st process! I get this massive from 4th process: 6 5 6 8 6 5 7 1 8 2
I'm 2nd process! after merge sort mass: 2 2 5 6 1 1 4 6 4 2 7 7 8 5 8 9 9 9 9 9
I'm 1st process! after merge sort mass: 1 2 2 2 4 6 6 5 6 7 8 8 8 0 0 0 0 0 0 3
I'm 1st process! I get this massive from 2nd process: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
| #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#define PART_SIZE 10
#define INTERMEDIATE_SIZE 20
#define GLOBAL_SIZE 40
void write_to_inputfile(char *filename);
void read_from_file(char *filename,int *array1,int *array2,int *array3,int *array4);
void write_to_outputfile(char *filename,int *array);
void shell_sort(int *array);
void merge_sort(int *array1,int *array2,int *result,int partsize);
void time_start();
void time_stop();
int arr1[PART_SIZE],arr2[PART_SIZE],arr3[PART_SIZE],arr4[PART_SIZE];
int in_arr1[INTERMEDIATE_SIZE],in_arr2[INTERMEDIATE_SIZE];
int final_array[GLOBAL_SIZE];
struct timeval tim;
double time_1,time_2;
int stat_val;
int data;
pid_t child_pid;
pid_t fork_result;
int file_pipes1[2];
int file_pipes2[2];
int file_pipes3[2];
int main(int argc,char *argv[])
{
if (argc != 3)
{
perror("program format: program <input file> <output file>");
exit(EXIT_FAILURE);
}
write_to_inputfile(argv[1]);
read_from_file(argv[1],arr1,arr2,arr3,arr4);
if(pipe(file_pipes1) == 0)
{
fork_result = fork();
if(fork_result == -1)
{
fprintf(stderr,"Fork failure ");
exit(EXIT_FAILURE);
}
if(fork_result == 0)
{
if(pipe(file_pipes2) == 0)
{
fork_result = fork();
if(fork_result == -1)
{
fprintf(stderr,"Fork failure ");
exit(EXIT_FAILURE);
}
if(fork_result == 0)
{
close(file_pipes1[1]);
close(file_pipes2[0]);
shell_sort(arr4);
printf("I'm 3rd process! after shell sort mass: ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr4[i]);
printf("\n");
data = write(file_pipes2[1],arr4,BUFSIZ);
close(file_pipes2[1]);
exit(EXIT_SUCCESS);
}
else
{
close(file_pipes2[1]);
close(file_pipes1[0]);
shell_sort(arr3);
printf("I'm 2nd process! after shell sort mass: ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr3[i]);
printf("\n");
child_pid = wait(&stat_val);
data = read(file_pipes2[0],arr4,BUFSIZ);
printf("I'm 2nd process! I get this massive from 3rd process: ");
i = 0;
for(;i < PART_SIZE;i++)
printf("%d ",arr4[i]);
printf("\n");
close(file_pipes2[0]);
merge_sort(arr3,arr4,in_arr2,PART_SIZE);
printf("I'm 2nd process! after merge sort mass: ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d ",in_arr2[i]);
printf("\n");
data = write(file_pipes1[1],in_arr2,BUFSIZ);
close(file_pipes1[1]);
exit(EXIT_SUCCESS);
}
}
}
else
{
if(pipe(file_pipes3) == 0)
{
fork_result = fork();
if(fork_result == -1)
{
fprintf(stderr,"Fork failure ");
exit(EXIT_FAILURE);
}
if(fork_result == 0)
{
close(file_pipes1[0]);
close(file_pipes1[1]);
close(file_pipes3[0]);
shell_sort(arr2);
printf("I'm 4th process! after shell sort mass: ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr2[i]);
printf("\n");
data = write(file_pipes3[1],arr2,BUFSIZ);
close(file_pipes3[1]);
exit(EXIT_SUCCESS);
}
else
{
close(file_pipes1[1]);
close(file_pipes3[1]);
shell_sort(arr1);
printf("I'm 1st process! after shell sort mass: ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr1[i]);
printf("\n");
child_pid = wait(&stat_val);
data = read(file_pipes3[0],arr2,BUFSIZ);
close(file_pipes3[0]);
printf("I'm 1st process! I get this massive from 4th process: ");
i = 0;
for(;i < PART_SIZE;i++)
printf("%d ",arr2[i]);
printf("\n");
merge_sort(arr1,arr2,in_arr1,INTERMEDIATE_SIZE);
printf("I'm 1st process! after merge sort mass: ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d ",in_arr1[i]);
printf("\n");
child_pid = wait(&stat_val);
data = read(file_pipes1[0],in_arr2,BUFSIZ);
printf("I'm 1st process! I get this massive from 2nd process: ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d ",in_arr2[i]);
printf("\n");
close(file_pipes1[0]);
merge_sort(in_arr1,in_arr2,final_array,INTERMEDIATE_SIZE);
write_to_outputfile(argv[2],final_array);
}
}
}
}
return EXIT_SUCCESS;
}
void write_to_inputfile(char *filename)
{
FILE *fo;
fo = fopen(filename,"w");
int size = GLOBAL_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fprintf(fo,"%d ",1 + rand() % 9);
fclose(fo);
}
void read_from_file(char *filename,int *arr1,int *arr2,int *arr3,int *arr4)
{
FILE *fo;
fo = fopen(filename,"r");
int size = PART_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fscanf(fo,"%d",arr1++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr2++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr3++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr4++);
fclose(fo);
}
void write_to_outputfile(char *filename,int *array)
{
FILE *fo;
fo = fopen(filename,"w");
int size = GLOBAL_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fprintf(fo,"%d ",*array++);
fclose(fo);
}
void shell_sort(int *array)
{
int step = PART_SIZE / 2;
int j;
while(step > 0)
{
int i = step;
for(;i < PART_SIZE - step;i++)
{
j = i;
while((j >= 0) && (array[j] > array[j + step]))
{
int buf = array[j];
array[j] = array[j + step];
array[j + step] = buf;
j--;
}
}
step /= 2;
}
}
void merge_sort(int *array1,int *array2,int *result,int partsize)
{
int i = 0,j = 0,index = 0;
while(i < partsize && j < partsize)
{
if(array1[i] > array2[j])
result[index++] = array2[j++];
else
result[index++] = array1[i++];
}
while(i < partsize)
result[index++] = array1[i++];
while(j < partsize)
result[index++] = array2[j++];
}
void time_start()
{
gettimeofday(&tim, NULL);
time_1 = tim.tv_sec + (tim.tv_usec/1000000.0);
}
void time_stop()
{
gettimeofday(&tim, NULL);
time_2 = tim.tv_sec + (tim.tv_usec/1000000.0);
} |
|
Добавлено через 23 часа 16 минут
Проблему,касающуюся передачи массивов через каналы,решил,теперь осталось найти ошибку в сортировке Шелла :
C | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| void shell_sort(int *array)
{
int step = PART_SIZE / 2;
int j;
while(step > 0)
{
int i = step;
for(;i < PART_SIZE - step;i++)
{
j = i;
while((j >= 0) && (array[j] > array[j + step]))
{
int buf = array[j];
array[j] = array[j + step];
array[j + step] = buf;
j--;
}
}
step /= 2;
}
} |
|
Добавлено через 22 минуты
Экспериментальным путём выяснил,что дело не в алгоритме сортировки @_@ что-то не даёт как-будто правильно сортировать,то ли в памяти куда-то не туда залезаю,то ли ещё что-то
вот вариант попроще сделал,в 3 процесса:
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
| #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#define PART_SIZE 5
#define INTERMEDIATE_SIZE 10
#define GLOBAL_SIZE 20
void write_to_inputfile(char *filename);
void read_from_file(char *filename,int *array1,int *array2,int *array3,int *array4);
void write_to_outputfile(char *filename,int *array);
void shell_sort(int *array,int n);
void merge_sort(int *array1,int *array2,int *result,int partsize);
void time_start();
void time_stop();
int arr1[PART_SIZE],arr2[PART_SIZE],arr3[PART_SIZE],arr4[PART_SIZE];
int in_arr1[INTERMEDIATE_SIZE];
int final_array[GLOBAL_SIZE];
struct timeval tim;
double time_1,time_2;
int stat_val;
int data;
pid_t child_pid;
pid_t fork_result;
int file_pipes1[2];
int file_pipes2[2];
int main(int argc,char *argv[])
{
int in_arr2[INTERMEDIATE_SIZE];
if (argc != 3)
{
perror("program format: program <input file> <output file>");
exit(EXIT_FAILURE);
}
write_to_inputfile(argv[1]);
read_from_file(argv[1],arr1,arr2,arr3,arr4);
if(pipe(file_pipes1) == 0)
{
fork_result = fork();
if(fork_result == -1)
{
fprintf(stderr,"Fork failure ");
exit(EXIT_FAILURE);
}
if(fork_result == 0)
{
if(pipe(file_pipes2) == 0)
{
fork_result = fork();
if(fork_result == -1)
{
fprintf(stderr,"Fork failure ");
exit(EXIT_FAILURE);
}
if(fork_result == 0)
{
close(file_pipes1[1]);
close(file_pipes1[0]);
close(file_pipes2[0]);
shell_sort(arr3,PART_SIZE);
printf("I'm 3rd process! after shell sort mass: ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr4[i]);
printf("\n");
data = write(file_pipes2[1],arr4,BUFSIZ);
close(file_pipes2[1]);
exit(EXIT_SUCCESS);
}
else
{
close(file_pipes1[0]);
close(file_pipes2[1]);
shell_sort(arr3,PART_SIZE);
printf("I'm 2nd process! after shell sort mass: ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr3[i]);
printf("\n");
child_pid = wait(&stat_val);
data = read(file_pipes2[0],arr4,BUFSIZ);
close(file_pipes2[0]);
printf("I'm 2nd process! i get this mass from 3rd : ");
i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr4[i]);
printf("\n");
merge_sort(arr3,arr4,in_arr2,PART_SIZE);
printf("I'm 2nd process! after merge sort : ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d",in_arr2[i]);
printf("\n");
data = write(file_pipes1[1],in_arr2,BUFSIZ);
close(file_pipes1[1]);
exit(EXIT_SUCCESS);
}
}
}
else
{
close(file_pipes1[1]);
shell_sort(arr1,PART_SIZE);
shell_sort(arr2,PART_SIZE);
printf("I'm 1st process! after shell sort mass:\n");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr1[i]);
printf("\n");
i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr2[i]);
printf("\n");
merge_sort(arr1,arr2,in_arr1,PART_SIZE);
printf("I'm 1st process! after merge sort : ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d",in_arr1[i]);
printf("\n");
child_pid = wait(&stat_val);
data = read(file_pipes1[0],in_arr2,BUFSIZ);
close(file_pipes1[0]);
printf("I'm 1st process! i get this mass from 2nd : ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d",in_arr2[i]);
printf("\n");
merge_sort(in_arr1,in_arr2,final_array,INTERMEDIATE_SIZE);
write_to_outputfile(argv[2],final_array);
}
}
return EXIT_SUCCESS;
}
void write_to_inputfile(char *filename)
{
FILE *fo;
fo = fopen(filename,"w");
int size = GLOBAL_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fprintf(fo,"%d ",1 + rand() % 9);
fclose(fo);
}
void read_from_file(char *filename,int *arr1,int *arr2,int *arr3,int *arr4)
{
FILE *fo;
fo = fopen(filename,"r");
int size = PART_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fscanf(fo,"%d",arr1++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr2++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr3++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr4++);
fclose(fo);
}
void write_to_outputfile(char *filename,int *array)
{
FILE *fo;
fo = fopen(filename,"w");
int size = GLOBAL_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fprintf(fo,"%d ",*array++);
fclose(fo);
}
void shell_sort(int* a, int n)
{
int step = n / 2;
int j;
while (step > 0)
{
int i = step;
for (; i < n - step; i++)
{
j = i;
while ((j >= 0) && (a[j]) > a[j + step])
{
int buf = a[j];
a[j] = a[j + step];
a[j + step] = buf;
j--;
}
}
step /= 2;
}
}
void merge_sort(int *array1,int *array2,int *result,int partsize)
{
int i = 0,j = 0,index = 0;
while(i < partsize && j < partsize)
{
if(array1[i] > array2[j])
result[index++] = array2[j++];
else
result[index++] = array1[i++];
}
while(i < partsize)
result[index++] = array1[i++];
while(j < partsize)
result[index++] = array2[j++];
}
void time_start()
{
gettimeofday(&tim, NULL);
time_1 = tim.tv_sec + (tim.tv_usec/1000000.0);
}
void time_stop()
{
gettimeofday(&tim, NULL);
time_2 = tim.tv_sec + (tim.tv_usec/1000000.0);
} |
|
Добавлено через 44 минуты
дальше - меньше,
решил сделать всё вообще в 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
| #include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#define PART_SIZE 10
#define INTERMEDIATE_SIZE 20
#define GLOBAL_SIZE 40
void write_to_inputfile(char *filename);
void read_from_file(char *filename,int *array1,int *array2,int *array3,int *array4);
void write_to_outputfile(char *filename,int *array);
void shell_sort(int *array,int n);
void merge_sort(int *array1,int *array2,int *result,int partsize);
void time_start();
void time_stop();
int arr1[PART_SIZE],arr2[PART_SIZE],arr3[PART_SIZE],arr4[PART_SIZE];
int in_arr1[INTERMEDIATE_SIZE],in_arr2[INTERMEDIATE_SIZE];
int final_array[GLOBAL_SIZE];
pid_t child_pid;
pid_t fork_result;
int file_pipes[2];
int stat_val;
int data;
struct timeval tim;
double time_1,time_2;
int main(int argc,char *argv[])
{
if (argc != 3)
{
perror("program format: program <input file> <output file>");
exit(EXIT_FAILURE);
}
write_to_inputfile(argv[1]);
read_from_file(argv[1],arr1,arr2,arr3,arr4);
if(pipe(file_pipes) == 0)
{
fork_result = fork();
if(fork_result == -1)
{
fprintf(stderr,"Fork failure ");
exit(EXIT_FAILURE);
}
}
if(fork_result == 0)
{
close(file_pipes[0]);
shell_sort(arr3,PART_SIZE);
shell_sort(arr4,PART_SIZE);
printf("I'm 2nd process! after shell sort mass:\n ");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr3[i]);
printf("\n");
i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr4[i]);
printf("\n");
merge_sort(arr3,arr4,in_arr2,PART_SIZE);
printf("I'm 2nd process! after merge sort : ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d",in_arr2[i]);
printf("\n");
data = write(file_pipes[1],in_arr2,BUFSIZ);
close(file_pipes[1]);
exit(EXIT_SUCCESS);
}
else
{
close(file_pipes[1]);
shell_sort(arr1,PART_SIZE);
shell_sort(arr2,PART_SIZE);
printf("I'm 1st process! after shell sort mass:\n");
int i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr1[i]);
printf("\n");
i = 0;
for(;i < PART_SIZE;i++)
printf("%d",arr2[i]);
printf("\n");
merge_sort(arr1,arr2,in_arr1,PART_SIZE);
printf("I'm 1st process! after merge sort : ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d",in_arr1[i]);
printf("\n");
child_pid = wait(&stat_val);
data = read(file_pipes[0],in_arr2,BUFSIZ);
close(file_pipes[0]);
printf("I'm 1st process! i get this mass from 2nd : ");
i = 0;
for(;i < INTERMEDIATE_SIZE;i++)
printf("%d",in_arr2[i]);
printf("\n");
merge_sort(in_arr1,in_arr2,final_array,INTERMEDIATE_SIZE);
write_to_outputfile(argv[2],final_array);
}
return EXIT_SUCCESS;
}
void write_to_inputfile(char *filename)
{
FILE *fo;
fo = fopen(filename,"w");
int size = GLOBAL_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fprintf(fo,"%d ",1 + rand() % 9);
fclose(fo);
}
void read_from_file(char *filename,int *arr1,int *arr2,int *arr3,int *arr4)
{
FILE *fo;
fo = fopen(filename,"r");
int size = PART_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fscanf(fo,"%d",arr1++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr2++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr3++);
size = PART_SIZE;
while(size--)
fscanf(fo,"%d",arr4++);
fclose(fo);
}
void write_to_outputfile(char *filename,int *array)
{
FILE *fo;
fo = fopen(filename,"w");
int size = GLOBAL_SIZE;
if(fo == 0)
{
perror(filename);
exit(EXIT_FAILURE);
}
while(size--)
fprintf(fo,"%d ",*array++);
fclose(fo);
}
void shell_sort(int* a, int n)
{
int step = n / 2;
int j;
while (step > 0)
{
int i = step;
for (; i < n - step; i++)
{
j = i;
while ((j >= 0) && (a[j]) > a[j + step])
{
int buf = a[j];
a[j] = a[j + step];
a[j + step] = buf;
j--;
}
}
step /= 2;
}
}
void merge_sort(int *array1,int *array2,int *result,int partsize)
{
int i = 0,j = 0,index = 0;
while(i < partsize && j < partsize)
{
if(array1[i] > array2[j])
result[index++] = array2[j++];
else
result[index++] = array1[i++];
}
while(i < partsize)
result[index++] = array1[i++];
while(j < partsize)
result[index++] = array2[j++];
}
void time_start()
{
gettimeofday(&tim, NULL);
time_1 = tim.tv_sec + (tim.tv_usec/1000000.0);
}
void time_stop()
{
gettimeofday(&tim, NULL);
time_2 = tim.tv_sec + (tim.tv_usec/1000000.0);
} |
|
0
|