home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / s / snip1292.zip / RG_SSORT.C < prev    next >
C/C++ Source or Header  |  1992-04-27  |  1KB  |  44 lines

  1. /*
  2. **  ssort()  --  Fast, small, qsort()-compatible Shell sort
  3. **
  4. **  by Ray Gardner,  public domain   5/90
  5. */
  6.  
  7. #include <stddef.h>
  8.  
  9. void ssort (const void *base,
  10.             size_t nel,
  11.             size_t width,
  12.             int (*comp)(const void *, const void *))
  13. {
  14.       size_t wnel, gap, wgap, i, j, k;
  15.       char *a, *b, tmp;
  16.  
  17.       wnel = width * nel;
  18.       for (gap = 0; ++gap < nel;)
  19.             gap *= 3;
  20.       while ( gap /= 3 )
  21.       {
  22.             wgap = width * gap;
  23.             for (i = wgap; i < wnel; i += width)
  24.             {
  25.                   for (j = i - wgap; ;j -= wgap)
  26.                   {
  27.                         a = j + (char *)base;
  28.                         b = a + wgap;
  29.                         if ( (*comp)(a, b) <= 0 )
  30.                               break;
  31.                         k = width;
  32.                         do
  33.                         {
  34.                               tmp = *a;
  35.                               *a++ = *b;
  36.                               *b++ = tmp;
  37.                         } while ( --k );
  38.                         if (j < wgap)
  39.                               break;
  40.                   }
  41.             }
  42.       }
  43. }
  44.