-
Notifications
You must be signed in to change notification settings - Fork 0
/
qs-4.c
209 lines (177 loc) · 4.23 KB
/
qs-4.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define NUM_THREADS 25000
typedef struct arr_num
{
int count;
int *array;
}arr_num;
double shared_x;
pthread_mutex_t lock_x;
void swap(int* a, int* b)
{
int aux;
aux = *a;
*a = *b;
*b = aux;
}
int divide(int *vec, int left, int right)
{
int i, j;
i = left;
for (j = left + 1; j <= right; ++j)
{
if (vec[j] < vec[left])
{
++i;
swap(&vec[i], &vec[j]);
}
}
swap(&vec[left], &vec[i]);
return i;
}
void quickSort(int *vec, int left, int right)
{
int r;
if (right > left)
{
r = divide(vec, left, right);
quickSort(vec, left, r - 1);
quickSort(vec, r + 1, right);
}
}
void quick_sort (int *a, int n) {
int i, j, p, t;
if (n < 2)
return;
p = a[n / 2];
for (i = 0, j = n - 1;; i++, j--) {
while (a[i] < p)
i++;
while (p < a[j])
j--;
if (i >= j)
break;
t = a[i];
a[i] = a[j];
a[j] = t;
}
quick_sort(a, i);
quick_sort(a + i, n - i);
}
void* quick_sort_m (void* parameters)
{
struct arr_num* arr = (struct arr_num*) parameters;
int i,j,p,t,th_num;
pthread_t thread[NUM_THREADS];
if (arr->count < 2)
return;
p=arr->array[arr->count/2];
for(i = 0,j=arr->count-1;; i++,j++)
{
while(arr->array[i]<p)
i++;
while (p<arr->array[j])
j--;
if (i >= j)
break;
t = arr->array[i];
arr->array[i] = arr->array[j];
arr->array[j] = t;
}
pthread_mutex_lock(&lock_x);
th_num++;
//quick_sort_m(arr);
pthread_create (&thread[th_num], NULL, &quick_sort_m,&arr);
pthread_join (thread[th_num], NULL);
pthread_mutex_unlock(&lock_x);
pthread_mutex_lock(&lock_x);
th_num++;
arr->array=arr->array+i;
arr->count=arr->count-i;
pthread_create (&thread[th_num], NULL, &quick_sort_m,&arr);
pthread_join (thread[th_num], NULL);
pthread_mutex_unlock(&lock_x);
}
int run_s(int num){
int a[num],i,n;
n = sizeof a / sizeof a[0];
for (i=0;i<num;i++)
a[i]=rand() % num + 1;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
printf("\n----------- sorted -----------\n");
//quickSort(a,0, n);
quick_sort(a, n);
for (i = 0; i <= n; i++)
printf("%d%s", a[i], i == n ? "\n" : " ");
return 0;
}
int run_m(int num){
struct arr_num an;
int r,j;
pthread_t thread[NUM_THREADS];
pthread_mutex_init(&lock_x, NULL);
an.count=num;
an.array= malloc(sizeof(int) * an.count);
for (r=0;r<an.count;r++)
{
an.array[r]=rand() % num - 1;
printf(" -- %d - %d -- \n",r,an.array[r]);
}
pthread_create (&thread[0], NULL, &quick_sort_m,&an);
pthread_join (thread[0], NULL);
printf("\n----------- sorted -----------\n");
for (j=0;j<an.count;j++)
printf(" -- %d - %d --\n",j,an.array[j]);
return 0;
}
void help(){
printf("\nUsage: qs [OPTION]... [SIZE]...\nSort with quicksort algoritm a random array of SIZE, SIZE and OPTION are mandatory,SIZE must be a integer, OPTION must be [-s/-m], a default run displays this help and exit.\n\nMandatory arguments to long options are mandatory for short options too.\n\t-m run with multiprocess support\n\t-s run without multiprocess support\n\t\t-h display this help and exit\n\t\t-v output version information and exit\n\n");
}
void vers(){
printf("\nqs 1.02\nCopyright (C) 2014 Free Software Foundation, Inc.\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law.\n\nWritten by Gonçalo Faria, Luis Franco, and Vitor Filipe \n\n");
}
int main (int argc, char *argv[]) {
int rtn,total,opt;
switch(argc){
case 2:
opt = getopt(argc, argv, "v");
if(opt == 'v')
{
vers();
rtn=0;
}
else
{
help();
rtn=1;
}
break;
case 3:
opt = getopt(argc, argv, "s:m:");
if(opt == 's')
{
total=atoi(argv[2]);
rtn=run_s(total);
}
else if (opt == 'm')
{
printf("2do\n");
total=atoi(argv[2]);
rtn=run_m(total);
}
else
{
help();
rtn=1;
}
break;
default:
help();
rtn=1;
break;
}
return rtn;
}