annotate quicksort/quicksort_test.cbc @ 2:50e23a4b2f40

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