-
Notifications
You must be signed in to change notification settings - Fork 0
/
qs-singleproc-args.c
65 lines (54 loc) · 1.48 KB
/
qs-singleproc-args.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
#include <stdio.h>
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);
}
int run(int num){
int a[num],i,n;
n = sizeof a / sizeof a[0];
for (i=0;i<num;i++)
a[i]=rand(num) % num;
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
printf("\n----------- sorted -----------\n");
quick_sort(a, n);
for (i = 0; i < n; i++)
printf("%d%s", a[i], i == n - 1 ? "\n" : " ");
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, --multiproc run with multiprocess support\n\t-s, --singleproc run without multiprocess support\n\t\t--help display this help and exit\n\t\t--version output version information and exit\n\n");
}
int main (int argc, char *argv[]) {
int rtn,total;
switch(argc){
case 2:
help();
rtn=0;
break;
case 3:
total=atoi(argv[2]);
rtn=run(total);
break;
default:
help();
rtn=0;
break;
}
return rtn;
}