home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_09 / 8n09024a < prev    next >
Text File  |  1990-06-20  |  745b  |  44 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define NUMELEM(a) (sizeof(a)/sizeof(a[0]))
  5.  
  6. main()
  7. {
  8.     int cmpia(const void *, const void *);
  9.     int array[] = {25, 3, 22, -5, 3, 24};
  10.     int i;
  11.  
  12.     qsort(&array[0], NUMELEM(array), sizeof(int), cmpia);
  13.  
  14.     printf("ascending integer order\n");
  15.     for (i = 0; i < NUMELEM(array); ++i)
  16.         printf("array[%d] = %2d\n", i, array[i]);
  17.  
  18.     return 0;
  19. }
  20.  
  21. /* compare ints in ascending order */
  22.  
  23. int cmpia(const void *pe1, const void *pe2)
  24. {
  25.     const int *pi1 = pe1;
  26.     const int *pi2 = pe2;
  27.  
  28.     if (*pi1 < *pi2)
  29.         return -1;
  30.     else if (*pi1 == *pi2)
  31.         return 0;
  32.     else
  33.         return 1;
  34. }
  35.  
  36. ascending integer order
  37. array[0] = -5
  38. array[1] =  3
  39. array[2] =  3
  40. array[3] = 22
  41. array[4] = 24
  42. array[5] = 25
  43.  
  44.