Mercurial > hg > CbC > CbC_gcc
comparison CbC-examples/quicksort/quicksort_test.cbc @ 22:0eb6cac880f0
add cbc example of quicksort.
author | kent <kent@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 13 Oct 2009 17:15:58 +0900 |
parents | |
children | 775dfe898662 |
comparison
equal
deleted
inserted
replaced
21:959d4c8c8abc | 22:0eb6cac880f0 |
---|---|
1 #include<stdio.h> | |
2 #include<stdlib.h> | |
3 #include<assert.h> | |
4 #include<unistd.h> | |
5 | |
6 #include"quicksort_test.h" | |
7 | |
8 void | |
9 random_initialize(int *v, int size, int min, int max) | |
10 { | |
11 int i; | |
12 int diff = max-min+1; | |
13 | |
14 for (i=0; i<size; i++) { | |
15 v[i] = min+random()%diff; | |
16 } | |
17 return; | |
18 } | |
19 | |
20 static void | |
21 print_array(int *v, int size) | |
22 { | |
23 int i; | |
24 printf("["); | |
25 for (i=0; i<size; i++) { | |
26 printf("%s%4d", (i%10==0)? "\n ":" ", v[i]); | |
27 } | |
28 printf(" ]\n"); | |
29 } | |
30 | |
31 void | |
32 starter() | |
33 { | |
34 int *target; | |
35 int size=100; | |
36 | |
37 target = (int*)malloc(sizeof(int)*size); | |
38 if (!target) { | |
39 perror("malloc"); | |
40 exit(1); | |
41 } | |
42 | |
43 random_initialize(target, size, 0, 90); | |
44 | |
45 print_array(target, size); | |
46 goto quicksort(target, 0, size-1, exit0, (void*)target); | |
47 | |
48 printf("bad region\n"); | |
49 } | |
50 | |
51 int | |
52 main(int argc, char **argv) | |
53 { | |
54 unsigned int seed=0; | |
55 int opt; | |
56 | |
57 while ((opt = getopt(argc, argv, "s:")) != -1) { | |
58 switch (opt) { | |
59 case 's': | |
60 seed = atoi(optarg); | |
61 break; | |
62 default: | |
63 fprintf(stderr, "Usage: %s [-t times] [-n sizeofarray] [-s seed]\n", argv[0]); | |
64 exit(1); | |
65 } | |
66 } | |
67 | |
68 srandom(seed); | |
69 starter(); | |
70 return 0; | |
71 } | |
72 | |
73 static int | |
74 check_sort(int *v, int size) | |
75 { | |
76 int i; | |
77 for (i=0; i<size-1; i++) { | |
78 if (v[i] > v[i+1]) | |
79 return 0; | |
80 } | |
81 return 1; | |
82 } | |
83 | |
84 __code | |
85 exit0(void *arg) | |
86 { | |
87 int *v = arg; | |
88 int b; | |
89 print_array(arg, 100); | |
90 b = check_sort(arg, 100); | |
91 if (b) { | |
92 printf("sorting successful!\n"); | |
93 exit(-1); | |
94 } else { | |
95 printf("sorting failure! \n"); | |
96 exit(0); | |
97 } | |
98 } | |
99 |