home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / 014r / plug1.zip / QSORT.C < prev   
Text File  |  1987-03-10  |  1KB  |  54 lines

  1.  
  2.  
  3.  
  4. /*    #define swmem swami
  5.  
  6.   int _nestr, _nestct;
  7. */
  8. void
  9. qsort (ary, ct, size, cfun)    /* My own Qsort  */
  10.   char *ary;
  11.   unsigned ct, size;
  12.   int (*cfun)();        /* Compare function  */
  13. {
  14.   char *lpt, *rpt;        /* Left ptr, right ptr    */
  15.   unsigned lc, rc, ec;        /* Left count, right count  */
  16.   int f;
  17.  
  18.     /*    if (++_nestr > _nestct) _nestct = _nestr;    */
  19.     while (ct > 1) {       /* Need at least 2       */
  20.        rpt = lpt = ary + size;
  21.        if (ct  == 2) {     /* Quickie for just 2     */
  22.         if (((*cfun)(lpt, ary)) < 0)  swmem (lpt, ary, size);
  23.         break;
  24.        }
  25.        swmem (ary, ary+(size*(ct>>1)), size);
  26.        ec = lc = rc = 0;
  27.        while (--ct)  {           /* Do for all */
  28.         f = (*cfun)(ary, rpt);
  29.         if (f <= 0)  {
  30.              if (f == 0) ++ec;
  31.              ++rc;               /* And count one  */
  32.         } else {
  33.              ++lc;             /* Count one to the left  */
  34.              if (rc)
  35.              swmem (lpt, rpt, size);  /* Swap  */
  36.              lpt += size;
  37.                 }
  38.         rpt += size;          /* Incr  right  */
  39.        }
  40.        if (lc) {
  41.            swmem (ary, lpt-size, size);
  42.            if (rc < lc) {
  43.             f = (rc != ec) ? rc : 0;
  44.             rc = lc; lc = f;
  45.             rpt = ary; ary = lpt; lpt = rpt;
  46.            }
  47.            if (lc > 1) qsort (ary, lc, size, cfun);
  48.        }
  49.        if (rc != ec)   { ct = rc; ary = lpt; } else break;
  50.     }
  51.    /*    --_nestr;    */
  52. }
  53.  
  54.