home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_04 / 1104108a < prev    next >
Text File  |  1993-02-05  |  548b  |  36 lines

  1. /* sort1.c: Sort integers with qsort() */
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5.  
  6. #define NELEMS 4
  7.  
  8. static int icomp(const void *, const void *);
  9.  
  10. main()
  11. {
  12.     size_t i;
  13.     int some_ints[NELEMS] = {40, 12, 37, 15};
  14.  
  15.     qsort(some_ints,NELEMS,sizeof some_ints[0], icomp);
  16.  
  17.     for (i = 0; i < NELEMS; ++i)
  18.         printf("%d\n",some_ints[i]);
  19.     return 0;
  20. }
  21.  
  22. static int icomp(const void *p1, const void *p2)
  23. {
  24.     int a = * (int *) p1;
  25.     int b = * (int *) p2;
  26.  
  27.     return a - b;
  28. }
  29.  
  30. /* Output:
  31. 12
  32. 15
  33. 37
  34. 40
  35. */
  36.