home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 161_01 / qsortm.c < prev    next >
C/C++ Source or Header  |  1985-08-29  |  1KB  |  52 lines

  1. /* qsortm - test the qsort function */
  2. #include "local.h"
  3. #include "cputim.h"
  4. static long n_compares = 0;
  5. static int a[10000] = {0};
  6. /* intcmp - compare two ints */
  7. int intcmp(pi, pj)
  8.     register int *pi, *pj;
  9.     {
  10.     ++n_compares;
  11.     if (*pi < *pj)
  12.         return (-1);
  13.     else if (*pi == *pj)
  14.         return (0);
  15.     else
  16.         return (1);
  17.     }                                                                                                                                  /*
  18. .PE
  19. .PS 32
  20. /* qsortm (main) - run the test */
  21. main(ac, av)
  22.     int ac;
  23.     char *av[];
  24.     {
  25.     int i;
  26.     int lim;
  27.     double nlogn;
  28.     double tim;    /* in usec */
  29.     cputim_t cputim();
  30.  
  31.     lim = atoi(av[1]);
  32.     printf("lim=%d\n", lim);
  33.     for (i = 0; i < lim; ++i)
  34.         a[i] = rand();
  35.     printf("start:\n");
  36.     cputim();
  37.     qsort((data_ptr)a, lim, sizeof(int), intcmp);
  38.     tim = 1e6 * (double)cputim() / CLOCK_TICKS_PER_SECOND;
  39.     printf("cpu time = %.3f (sec)\n", tim / 1e6);
  40.     nlogn = lim * log((double)lim) / log(2.);
  41.     printf("n_compares = %ld\n", n_compares);
  42.     printf("cpu time = %.3f (usec) per compare\n", tim / n_compares);
  43.     printf("log N = %.3f\n", log((double)lim) / log(2.));
  44.     printf("N log N = %.3f\n", nlogn);
  45.     printf("cpu time = %.2f N log N (usec)\n", tim / nlogn);
  46.     printf("ratio of n_compares to N log N = %.3f\n\n", n_compares / nlogn);
  47.     for (i = 0; i < lim-1; ++i)
  48.         if (a[i] > a[i+1])
  49.             error("bad value", "");
  50.     exit(0);
  51.     }
  52.