Category : HD Utilities
Archive   : PLUG12.ZIP
Filename : QSORT.C

 
Output of file : QSORT.C contained in archive : PLUG12.ZIP




/* #define swmem swami

int _nestr, _nestct;
*/
void
qsort (ary, ct, size, cfun) /* My own Qsort */
char *ary;
unsigned ct, size;
int (*cfun)(); /* Compare function */
{
char *lpt, *rpt; /* Left ptr, right ptr */
unsigned lc, rc, ec; /* Left count, right count */
int f;

/* if (++_nestr > _nestct) _nestct = _nestr; */
while (ct > 1) { /* Need at least 2 */
rpt = lpt = ary + size;
if (ct == 2) { /* Quickie for just 2 */
if (((*cfun)(lpt, ary)) < 0) swmem (lpt, ary, size);
break;
}
swmem (ary, ary+(size*(ct>>1)), size);
ec = lc = rc = 0;
while (--ct) { /* Do for all */
f = (*cfun)(ary, rpt);
if (f <= 0) {
if (f == 0) ++ec;
++rc; /* And count one */
} else {
++lc; /* Count one to the left */
if (rc)
swmem (lpt, rpt, size); /* Swap */
lpt += size;
}
rpt += size; /* Incr right */
}
if (lc) {
swmem (ary, lpt-size, size);
if (rc < lc) {
f = (rc != ec) ? rc : 0;
rc = lc; lc = f;
rpt = ary; ary = lpt; lpt = rpt;
}
if (lc > 1) qsort (ary, lc, size, cfun);
}
if (rc != ec) { ct = rc; ary = lpt; } else break;
}
/* --_nestr; */
}