home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / RG_SSORT.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  1KB  |  47 lines

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