|
@ -32,7 +32,7 @@ |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
|
|
#if defined(LIBC_SCCS) && !defined(lint) |
|
|
static char *rcsid = "$OpenBSD: qsort.c,v 1.4 1996/08/19 08:33:42 tholo Exp $"; |
|
|
|
|
|
|
|
|
static char *rcsid = "$OpenBSD: qsort.c,v 1.5 1997/06/20 11:19:38 deraadt Exp $"; |
|
|
#endif /* LIBC_SCCS and not lint */ |
|
|
#endif /* LIBC_SCCS and not lint */ |
|
|
|
|
|
|
|
|
#include <sys/types.h> |
|
|
#include <sys/types.h> |
|
@ -65,7 +65,7 @@ swapfunc(a, b, n, swaptype) |
|
|
char *a, *b; |
|
|
char *a, *b; |
|
|
int n, swaptype; |
|
|
int n, swaptype; |
|
|
{ |
|
|
{ |
|
|
if(swaptype <= 1) |
|
|
|
|
|
|
|
|
if (swaptype <= 1) |
|
|
swapcode(long, a, b, n) |
|
|
swapcode(long, a, b, n) |
|
|
else |
|
|
else |
|
|
swapcode(char, a, b, n) |
|
|
swapcode(char, a, b, n) |
|
@ -104,16 +104,16 @@ qsort(aa, n, es, cmp) |
|
|
loop: SWAPINIT(a, es); |
|
|
loop: SWAPINIT(a, es); |
|
|
swap_cnt = 0; |
|
|
swap_cnt = 0; |
|
|
if (n < 7) { |
|
|
if (n < 7) { |
|
|
for (pm = a + es; pm < (char *) a + n * es; pm += es) |
|
|
|
|
|
|
|
|
for (pm = (char *)a + es; pm < (char *) a + n * es; pm += es) |
|
|
for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
|
|
for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
|
|
pl -= es) |
|
|
pl -= es) |
|
|
swap(pl, pl - es); |
|
|
swap(pl, pl - es); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
pm = a + (n / 2) * es; |
|
|
|
|
|
|
|
|
pm = (char *)a + (n / 2) * es; |
|
|
if (n > 7) { |
|
|
if (n > 7) { |
|
|
pl = a; |
|
|
|
|
|
pn = a + (n - 1) * es; |
|
|
|
|
|
|
|
|
pl = (char *)a; |
|
|
|
|
|
pn = (char *)a + (n - 1) * es; |
|
|
if (n > 40) { |
|
|
if (n > 40) { |
|
|
d = (n / 8) * es; |
|
|
d = (n / 8) * es; |
|
|
pl = med3(pl, pl + d, pl + 2 * d, cmp); |
|
|
pl = med3(pl, pl + d, pl + 2 * d, cmp); |
|
@ -123,9 +123,9 @@ loop: SWAPINIT(a, es); |
|
|
pm = med3(pl, pm, pn, cmp); |
|
|
pm = med3(pl, pm, pn, cmp); |
|
|
} |
|
|
} |
|
|
swap(a, pm); |
|
|
swap(a, pm); |
|
|
pa = pb = a + es; |
|
|
|
|
|
|
|
|
pa = pb = (char *)a + es; |
|
|
|
|
|
|
|
|
pc = pd = a + (n - 1) * es; |
|
|
|
|
|
|
|
|
pc = pd = (char *)a + (n - 1) * es; |
|
|
for (;;) { |
|
|
for (;;) { |
|
|
while (pb <= pc && (r = cmp(pb, a)) <= 0) { |
|
|
while (pb <= pc && (r = cmp(pb, a)) <= 0) { |
|
|
if (r == 0) { |
|
|
if (r == 0) { |
|
@ -151,14 +151,14 @@ loop: SWAPINIT(a, es); |
|
|
pc -= es; |
|
|
pc -= es; |
|
|
} |
|
|
} |
|
|
if (swap_cnt == 0) { /* Switch to insertion sort */ |
|
|
if (swap_cnt == 0) { /* Switch to insertion sort */ |
|
|
for (pm = a + es; pm < (char *) a + n * es; pm += es) |
|
|
|
|
|
|
|
|
for (pm = (char *) a + es; pm < (char *) a + n * es; pm += es) |
|
|
for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
|
|
for (pl = pm; pl > (char *) a && cmp(pl - es, pl) > 0; |
|
|
pl -= es) |
|
|
pl -= es) |
|
|
swap(pl, pl - es); |
|
|
swap(pl, pl - es); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
pn = a + n * es; |
|
|
|
|
|
|
|
|
pn = (char *)a + n * es; |
|
|
r = min(pa - (char *)a, pb - pa); |
|
|
r = min(pa - (char *)a, pb - pa); |
|
|
vecswap(a, pb - r, r); |
|
|
vecswap(a, pb - r, r); |
|
|
r = min(pd - pc, pn - pd - es); |
|
|
r = min(pd - pc, pn - pd - es); |
|
|