home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / BDSC / BDSC-4 / BDSLIB.ARK / QSORT.C < prev    next >
Text File  |  1983-07-15  |  896b  |  38 lines

  1. /*
  2.  * qsort
  3.  * This function is exactly as documented in the BDS C Version
  4.  * 1.5 Users Manual and as swiped from K & P.
  5.  */
  6.  
  7. #define    MAX_QSORT_WIDTH    513    /* Largest object qsort can sort */
  8.  
  9. qsort(base, nel, width, compar)
  10. char *base; int (*compar)();
  11. unsigned width,nel;
  12. {    int i, j;
  13.     unsigned gap, ngap, t1;
  14.     int jd, t2;
  15.  
  16.     t1 = nel * width;
  17.     for (ngap = nel / 2; ngap > 0; ngap /= 2) {
  18.        gap = ngap * width;
  19.        t2 = gap + width;
  20.        jd = base + gap;
  21.        for (i = t2; i <= t1; i += width)
  22.           for (j =  i - t2; j >= 0; j -= gap) {
  23.         if ((*compar)(base+j, jd+j) <=0) break;
  24.              _swp(width, base+j, jd+j);
  25.           }
  26.     }
  27. }
  28.  
  29. _swp(w,a,b)
  30. char *a,*b;
  31. unsigned w;
  32. {
  33.     char swapbuf[MAX_QSORT_WIDTH];
  34.     movmem(a,swapbuf,w);
  35.     movmem(b,a,w);
  36.     movmem(swapbuf,b,w);
  37. }
  38.