#include #include #include #include #ifndef HZ #define HZ 60 #endif #ifdef atarist long _stksize =-1L; #endif #ifdef __STDC__ void *malloc(unsigned long n); long random(void); long atol(char *); #else #define void char void *malloc(); long random(); long atol(); #endif #ifdef __hpux #define random rand #endif int compare(p1, p2) long *p1, *p2; { return (int)(*p1 - *p2); } int dcompare(p1, p2) double *p1, *p2; { double q1 = *p1, q2 = *p2; if(q1 < q2) return -1; else if(q1 == q2) return 0; else return 1; } long tcase[] = {101, 100, 99, 3, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, -1, -99, -100, -101}; long ntcase = sizeof(tcase)/sizeof(long); int main(argc, argv) int argc; char **argv; { register long i; register long siz; register unsigned long start, tot, grand; long *a; double *d; struct tms t; siz = (argc > 1)? atol(*++argv) : 10000; if(( a = (long *)malloc((unsigned long)(siz * sizeof(long)))) == (long *)NULL) { fprintf(stderr,"No mem\n"); exit(1); } /* printf("%ld elems\n", siz); */ /* case 1*/ for(i = 0 ; i < siz; i++) a[i] = siz-i; times(&t); start = t.tms_utime; qsort((char *)a, siz, sizeof(long), compare); times(&t); tot = (t.tms_utime - start); for(i = 1; i < siz; i++) { if(a[i-1] > a[i]) { fprintf(stderr,"Case 1: Not sorted at %ld (%ld %ld)\n", i, a[i-1], a[i]); return(2); } } /* printf("case 1 %ld secs (%ld)\n", tot/HZ, tot); */ grand = tot; /* random */ for(i = 0; i < siz; i++) a[i] =random() ; times(&t); start = t.tms_utime; qsort((char *)a, siz, sizeof(long), compare); times(&t); tot = (t.tms_utime - start); for(i = 1; i < siz; i++) { if(a[i-1] > a[i]) { fprintf(stderr,"Random: Not sorted at %ld (%ld %ld)\n", i, a[i-1], a[i]); return(3); } } /* printf("Rand %ld secs (%ld)\n", tot/HZ, tot); */ grand += tot; free(a); /* tcase */ times(&t); start = t.tms_utime; qsort((char *)tcase, ntcase, sizeof(long), compare); times(&t); tot = (t.tms_utime - start); for(i = 1; i < ntcase; i++) { if(tcase[i-1] > tcase[i]) { fprintf(stderr,"Tcase: Not sorted at %ld (%ld %ld)\n", i, tcase[i-1], tcase[i]); exit(3); } } /* printf("Tcase %ld secs (%ld)\n", tot/HZ, tot); */ grand += tot; /* double */ if(( d = (double *)malloc((unsigned long)(siz * sizeof(double)))) == (double *)NULL) { fprintf(stderr,"No mem\n"); exit(1); } for(i = 0; i < siz; i++) d[i] =random() ; times(&t); start = t.tms_utime; qsort((char *)d, siz, sizeof(double), dcompare); times(&t); tot = (t.tms_utime - start); for(i = 1; i < siz; i++) { if(d[i-1] > d[i]) { fprintf(stderr,"Double: Not sorted at %ld (%f %f)\n", i, d[i-1], d[i]); exit(3); } } /* printf("Double %ld secs (%ld)\n", tot/HZ, tot); */ grand += tot; printf("Total %ld secs (%ld)\n", grand/HZ, grand); return 0; }