22 #include "medians_1D.h"
33 #define BIG_NUM (1024*1024)
36 #define MAX_ARRAY_VALUE 1024
42 #define odd(x) ((x)&1)
45 void bench(
int,
size_t);
46 int compare(
const void *,
const void*);
49 pixelvalue
select_k(
int, pixelvalue *,
int);
51 void bench(
int verbose,
size_t array_size)
60 pixelvalue * array_init,
71 printf(
"generating element values...\n");
74 if (array_size<1) array_size =
BIG_NUM;
77 printf(
"array size: %ld\n", (
long)array_size);
79 printf(
"%ld\t", (
long)array_size);
82 array_init = malloc(array_size *
sizeof(pixelvalue));
83 array = malloc(array_size *
sizeof(pixelvalue));
85 if (array_init==NULL || array==NULL) {
86 printf(
"memory allocation failure: aborting\n");
90 for (i=0 ; i<array_size; i++) {
96 memcpy(array, array_init, array_size *
sizeof(pixelvalue));
98 printf(
"quick select :\t");
103 elapsed = (double)(clock() - chrono) / (
double)CLOCKS_PER_SEC;
105 printf(
"%5.3f sec\t", elapsed);
107 printf(
"med %g\n", (
double)med[mednum]);
110 printf(
"%5.3f\t", elapsed);
116 memcpy(array, array_init, array_size *
sizeof(pixelvalue));
118 printf(
"Wirth median :\t");
122 med[mednum] =
wirth(array, array_size);
123 elapsed = (double)(clock() - chrono) / (
double)CLOCKS_PER_SEC;
125 printf(
"%5.3f sec\t", elapsed);
127 printf(
"med %g\n", (
double)med[mednum]);
130 printf(
"%5.3f\t", elapsed);
136 memcpy(array, array_init, array_size *
sizeof(pixelvalue));
138 printf(
"AHU median :\t");
143 elapsed = (double)(clock() - chrono) / (
double)CLOCKS_PER_SEC;
145 printf(
"%5.3f sec\t", elapsed);
147 printf(
"med %g\n", (
double)med[mednum]);
150 printf(
"%5.3f\t", elapsed);
156 memcpy(array, array_init, array_size *
sizeof(pixelvalue));
158 printf(
"torben :\t");
162 med[mednum] =
torben(array, array_size);
163 elapsed = (double)(clock() - chrono) / (
double)CLOCKS_PER_SEC;
165 printf(
"%5.3f sec\t", elapsed);
167 printf(
"med %g\n", (
double)med[mednum]);
170 printf(
"%5.3f\t", elapsed);
176 memcpy(array, array_init, array_size *
sizeof(pixelvalue));
178 printf(
"fast pixel sort :\t");
183 elapsed = (double)(clock() - chrono) / (
double)CLOCKS_PER_SEC;
185 if (
odd(array_size)) {
186 med[mednum] = array[array_size/2];
188 med[mednum] = array[(array_size/2) -1];
192 printf(
"%5.3f sec\t", elapsed);
194 printf(
"med %g\n", (
double)med[mednum]);
197 printf(
"%5.3f\t", elapsed);
206 if (fabs(med[i-1] - med[i]) > 10 * FLT_EPSILON) {
207 printf(
"diverging median values!\n");
218 {
return ( *(pixelvalue*)f1 > *(pixelvalue*)f2) ? 1 : -1 ; }
231 #define PIX_SWAP(a,b) { pixelvalue temp=(a);(a)=(b);(b)=temp; }
232 #define PIX_STACK_SIZE 50
251 for (j=l+1 ; j<=ir ; j++) {
253 for (i=j-1 ; i>=1 ; i--) {
254 if (pix_arr[i-1] <= a)
break;
255 pix_arr[i] = pix_arr[i-1];
259 if (j_stack == 0)
break;
260 ir = i_stack[j_stack-- -1];
261 l = i_stack[j_stack-- -1];
265 if (pix_arr[l] > pix_arr[ir-1]) {
268 if (pix_arr[l-1] > pix_arr[ir-1]) {
269 PIX_SWAP(pix_arr[l-1], pix_arr[ir-1])
271 if (pix_arr[l] > pix_arr[l-1]) {
278 do i++;
while (pix_arr[i-1] < a);
279 do j--;
while (pix_arr[j-1] > a);
281 PIX_SWAP(pix_arr[i-1], pix_arr[j-1]);
283 pix_arr[l-1] = pix_arr[j-1];
287 printf(
"stack too small in pixel_qsort: aborting");
291 i_stack[j_stack-1] = ir;
292 i_stack[j_stack-2] = i;
295 i_stack[j_stack-1] = j-1;
296 i_stack[j_stack-2] = l;
303 #undef PIX_STACK_SIZE
316 pixelvalue
select_k(
int k, pixelvalue * list,
int n)
325 if (n==1)
return list[0];
327 for (i=0 ; i<n ; i++) {
330 }
else if (fabs(list[i] - p) < 10 * FLT_EPSILON) {
337 S = malloc(n1*
sizeof(pixelvalue));
339 for (i=0 ; i<n ; i++) {
340 if (list[i]<p) S[j++] = list[i];
346 S = malloc(n3 *
sizeof(pixelvalue));
348 for (i=0 ; i<n ; i++) {
349 if (list[i]>p) S[j++] = list[i];
371 int main(
int argc,
char * argv[])
379 printf(
"%s <n>\n", argv[0]);
380 printf(
"\tif n=1 the output is verbose for one attempt\n");
381 printf(
"\tif n>1 the output reads:\n");
382 printf(
"\t# of elements | method1 | method2 | ...\n");
384 printf(
"%s <from> <to> <step>\n", argv[0]);
385 printf(
"\twill loop over the number of elements in input\n");
391 count = atoi(argv[1]);
395 printf(
"Size\tQS\tWirth\tAHU\tTorben\tpixel sort\n");
396 for (i=0 ; i<atoi(argv[1]) ; i++) {
400 }
else if (argc==4) {
401 from = atoi(argv[1]);
403 step = atoi(argv[3]);
404 for (count=from ; count<=to ; count+=step) {
int main(int argc, char *argv[])
Main driver demo for median search routines.
int compare(const void *, const void *)
This function is only useful to the qsort() routine.
pixelvalue select_k(int, pixelvalue *, int)
Called by median_AHU()
static const char rcsid[]
#define BIG_NUM
Number of elements in the target array.
#define odd(x)
Macro to determine an integer's oddness.
pixelvalue median_AHU(pixelvalue *, int)
This function uses select_k to find the median.
void pixel_qsort(pixelvalue *, int)
Old and supposedly optimized quicksort algorithm.
#define MAX_ARRAY_VALUE
Random test data; generated values are in [0...MAX_ARRAY_VALUE-1].
#define N_METHODS
Number of search methods tested.