home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 183_01 / qsort.c < prev    next >
Text File  |  1985-10-09  |  1KB  |  52 lines

  1. #include <search.h>
  2. #include <stdio.h>
  3.  
  4. int compare ();  /* declare compare function */
  5.  
  6. /* define and initial array */
  7. int array[10] = { 2, 3, 2, 6, 8, 1, 9, 8 ,5, 4 };
  8.  
  9.  
  10. main()  /* qsort the 10  pre given ramdom integers */
  11. {
  12.     int num   = 10 ;          /* 10 integers */
  13.     int     i =  0 ;          /* array element starter*/
  14.      
  15.  
  16.     /* print the array before we sort it. */
  17.      printf ("before QSORT : \n");
  18.      while ( num-- > 0 )
  19.         printf ("%3d \n", array[i++]);
  20.  
  21.  
  22.     num = 10;  /* reset the total element count */
  23.  
  24.     /* call the library routine "QSORT" */
  25.      qsort(array, num, sizeof(int), compare);
  26.  
  27.  
  28.  
  29.     /* print the array after we sort it. */
  30.     i = 0;  /* reset the array element count */
  31.      printf ("after QSORT : \n");
  32.     while ( num-- > 0 )
  33.         printf ("%3d \n", array[i++]);
  34.  
  35. }
  36.  
  37.  
  38.  
  39. int compare (num1, num2)   /* little function for QSORT */
  40.  
  41. int *num1, *num2;  /* initial the POINTERs of the parameter  */
  42.  
  43. {
  44.     /*  return value :
  45.         negative   when   num1 < num2
  46.              0          when   num1 = num2
  47.              positive   when   num1 > num2   */
  48.  
  49.  
  50.     return( *num1 - *num2 );
  51. }
  52.