| 1 |
#include <stdio.h> |
| 2 |
#include <stdlib.h> |
| 3 |
|
| 4 |
/* |
| 5 |
Al Danial April 25 2000 |
| 6 |
*/ |
| 7 |
|
| 8 |
#define ELEMENTS 1000 |
| 9 |
|
| 10 |
static int compar_string(const void *a, const void *b){ |
| 11 |
return (strcmp( (char *)a, (char *)b)); |
| 12 |
} |
| 13 |
|
| 14 |
int comp(const void *a, const void *b ) { |
| 15 |
return *(int *)a - * (int *)b; |
| 16 |
} |
| 17 |
|
| 18 |
main(){ |
| 19 |
|
| 20 |
int x, i; |
| 21 |
char *string; |
| 22 |
struct sort_test_t { |
| 23 |
int s; |
| 24 |
} ; |
| 25 |
|
| 26 |
struct sort_test_t sort_test[ELEMENTS]; |
| 27 |
|
| 28 |
/* inititalize the array */ |
| 29 |
for (i=0;i<ELEMENTS;i++) { |
| 30 |
/* produce a random variable */ |
| 31 |
x=1+(100000.0*rand()/(RAND_MAX+1.0)); |
| 32 |
/* load the variable into the string as an array */ |
| 33 |
sort_test[i].s = (int) x; |
| 34 |
/* |
| 35 |
printf("unsorted %d %d\n", i, sort_test[i].s); |
| 36 |
*/ |
| 37 |
} |
| 38 |
|
| 39 |
/* sort the array */ |
| 40 |
qsort(sort_test, ELEMENTS, sizeof(sort_test[0]), &comp); |
| 41 |
|
| 42 |
/* output the sorted array */ |
| 43 |
for (i=0;i<ELEMENTS;i++) { |
| 44 |
printf("sorted %d %d\n", i, sort_test[i].s); |
| 45 |
} |
| 46 |
|
| 47 |
} |