-
Notifications
You must be signed in to change notification settings - Fork 0
/
mySort.c
315 lines (257 loc) · 8.26 KB
/
mySort.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
307
308
309
310
311
312
313
314
315
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/time.h>
struct Sort_Parameters {
char **words;
int start;
int end;
};
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int ThreadCount = 0;
int ThreadStatus();
void Merge(char **words, int start, int middle, int end);
void MergeSort(char **words, int start, int end);
void *ParallelMergeSort(void *args);
void QuickSort(char **words, int start, int end);
void *ParallelQuickSort(void *args);
int Partition(char **words, int start, int end);
void Swap(char **words, int i, int j);
int main(int argc, char *argv[]) {
struct timeval start, end;
double execution_time;
gettimeofday(&start, NULL);
if (argc != 5) {
printf("\nMissing arguments\n");
printf("\nFormat: <InputFile.txt> <OutputFile.txt> <Thread Count> <Sorting Algorithm (merge, quick)>\n\n");
return 1;
}
char *InputFile = argv[1];
char *OutputFile = argv[2];
ThreadCount = atoi(argv[3]);
if (ThreadCount < 0) {
printf("\nInvalid Thread Count!\n");
return 1;
}
char *SortingAlgorithm = argv[4];
printf("\nInput File: %s\n", InputFile);
printf("Output File: %s\n", OutputFile);
printf("Thread Count: %d\n", ThreadCount);
printf("Sorting Algorithm: %s\n", SortingAlgorithm);
FILE *input_file;
input_file = fopen(InputFile, "r");
if (input_file == NULL) {
printf("\nFailed to open Input File: %s\n", InputFile);
return 1;
}
char **words = NULL;
char word[100];
int word_count = 0;
while (fscanf(input_file, "%s", word) != EOF) {
words = (char **)realloc(words, (word_count + 1) * sizeof(char *));
if (words == NULL) {
printf("\nMemory allocation failed!\n");
return 1;
}
words[word_count] = strdup(word);
word_count++;
}
fclose(input_file);
struct Sort_Parameters parameters;
parameters.words = words;
parameters.start = 0;
parameters.end = word_count - 1;
if (strcmp(SortingAlgorithm, "merge") == 0) {
if (ThreadCount > 0) {
ParallelMergeSort(¶meters);
} else {
MergeSort(parameters.words, parameters.start, parameters.end);
}
} else if (strcmp(SortingAlgorithm, "quick") == 0) {
if (ThreadCount > 0) {
ParallelQuickSort(¶meters);
} else {
QuickSort(parameters.words, parameters.start, parameters.end);
}
} else {
printf("\nInvalid algorithm. Available algorithms: merge, quick\n\n");
return 1;
}
FILE *output_file;
int i;
output_file = fopen(OutputFile, "w");
if (output_file == NULL) {
printf("\nFailed to open Output File: %s\n", OutputFile);
return 1;
}
for (i = 0; i < word_count; i++) {
fprintf(output_file, "%s\n", words[i]);
free(words[i]);
}
fclose(output_file);
free(words);
printf("\nSorting Completed.\n");
gettimeofday(&end, NULL);
execution_time = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / 1000000.0;
printf("\nProgram execution time: %.6f seconds\n\n", execution_time);
return 0;
}
int ThreadStatus() {
pthread_mutex_lock(&mutex);
if (ThreadCount > 0) {
ThreadCount -= 1;
pthread_mutex_unlock(&mutex);
return 1;
} else {
pthread_mutex_unlock(&mutex);
return 0;
}
}
void *ParallelMergeSort(void *args) {
struct Sort_Parameters *p = (struct Sort_Parameters *)args;
if (p->start < p->end) {
struct Sort_Parameters *params_1 = malloc(sizeof(struct Sort_Parameters));
struct Sort_Parameters *params_2 = malloc(sizeof(struct Sort_Parameters));
if (params_1 == NULL || params_2 == NULL) {
printf("\nMemory allocation failed!\n");
return NULL;
}
params_1->words = p->words;
params_1->start = p->start;
params_1->end = p->end;
params_2->words = p->words;
params_2->start = p->start;
params_2->end = p->end;
int middle = (p->start + p->end) / 2;
params_1->end = middle;
params_2->start = middle + 1;
pthread_t thread_1;
pthread_t thread_2;
if (ThreadStatus()) {
pthread_create(&thread_1, NULL, ParallelMergeSort, params_1);
} else {
MergeSort(params_1->words, params_1->start, params_1->end);
}
if (ThreadStatus()) {
pthread_create(&thread_2, NULL, ParallelMergeSort, params_2);
} else {
MergeSort(params_2->words, params_2->start, params_2->end);
}
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
Merge(params_1->words, params_1->start, middle, params_2->end);
free(params_1);
free(params_2);
return NULL;
}
}
void MergeSort(char **words, int start, int end) {
if (start < end) {
int middle = (start + end) / 2;
MergeSort(words, start, middle);
MergeSort(words, middle + 1, end);
Merge(words, start, middle, end);
}
}
void Merge(char **words, int start, int middle, int end) {
int i, j, k;
int n1 = middle - start + 1;
int n2 = end - middle;
char **Left = (char **)malloc(n1 * sizeof(char *));
char **Right = (char **)malloc(n2 * sizeof(char *));
if (Left == NULL || Right == NULL) {
printf("\nMemory allocation failed!\n");
exit(1);
}
for (i = 0; i < n1; i++) {
Left[i] = words[start + i];
}
for (j = 0; j < n2; j++) {
Right[j] = words[middle + 1 + j];
}
i = 0;
j = 0;
k = start;
while (i < n1 && j < n2) {
if (strcasecmp(Left[i], Right[j]) <= 0) {
words[k] = Left[i];
i++;
} else {
words[k] = Right[j];
j++;
}
k++;
}
while (i < n1) {
words[k] = Left[i];
i++;
k++;
}
while (j < n2) {
words[k] = Right[j];
j++;
k++;
}
free(Left);
free(Right);
}
void *ParallelQuickSort(void *args) {
struct Sort_Parameters *p = (struct Sort_Parameters *)args;
if (p->start < p->end) {
int i = Partition(p->words, p->start, p->end);
struct Sort_Parameters *params_1 = malloc(sizeof(struct Sort_Parameters));
struct Sort_Parameters *params_2 = malloc(sizeof(struct Sort_Parameters));
if (params_1 == NULL || params_2 == NULL) {
printf("\nMemory allocation failed!\n");
return NULL;
}
params_1->words = p->words;
params_1->start = p->start;
params_1->end = i - 1;
params_2->words = p->words;
params_2->start = i + 1;
params_2->end = p->end;
pthread_t thread_1;
pthread_t thread_2;
if (ThreadStatus()) {
pthread_create(&thread_1, NULL, ParallelQuickSort, params_1);
} else {
QuickSort(params_1->words, params_1->start, params_1->end);
}
if (ThreadStatus()) {
pthread_create(&thread_2, NULL, ParallelQuickSort, params_2);
} else {
QuickSort(params_2->words, params_2->start, params_2->end);
}
pthread_join(thread_1, NULL);
pthread_join(thread_2, NULL);
free(params_1);
free(params_2);
}
return NULL;
}
void QuickSort(char **words, int start, int end) {
if (start < end) {
int i = Partition(words, start, end);
QuickSort(words, start, i - 1);
QuickSort(words, i + 1, end);
}
}
int Partition(char **words, int start, int end) {
char *pivot = words[end];
int i = (start - 1);
for (int j = start; j <= end; j++) {
if (strcasecmp(words[j], pivot) < 0) {
i++;
Swap(words, i, j);
}
}
Swap(words, i + 1, end);
return (i + 1);
}
void Swap(char **words, int i, int j) {
char *temp = words[i];
words[i] = words[j];
words[j] = temp;
}