2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Peter McIlroy and by Dan Bernstein at New York University,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
42 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
43 * Use radixsort(a, n, trace, endchar) for this case.
45 * For stable sorting (using N extra pointers) use sradixsort(), which calls
48 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
49 * "Engineering Radix Sort".
52 #include <sys/types.h>
62 static inline void simplesort
63 (const u_char **, int, int, const u_char *, u_int);
64 static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
65 static void r_sort_b(const u_char **, const u_char **, int, int,
66 const u_char *, u_int);
68 #define THRESHOLD 20 /* Divert to simplesort(). */
69 #define SIZE 512 /* Default stack size. */
74 for (c = 0; c < endch; c++) \
77 for (c++; c < 256; c++) \
83 if (endch != 0 && endch != 255) { \
91 radixsort(a, n, tab, endch)
92 const u_char **a, *tab;
101 r_sort_a(a, n, 0, tr, endch);
106 sradixsort(a, n, tab, endch)
107 const u_char **a, *tab;
111 const u_char *tr, **ta;
117 simplesort(a, n, 0, tr, endch);
119 if ((ta = malloc(n * sizeof(a))) == NULL)
121 r_sort_b(a, ta, n, 0, tr, endch);
127 #define empty(s) (s >= sp)
128 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
129 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
130 #define swap(a, b, t) t = a, a = b, b = t
132 /* Unstable, in-place sort. */
134 r_sort_a(a, n, i, tr, endch)
140 static int count[256], nc, bmin;
142 const u_char **ak, *r;
143 stack s[SIZE], *sp, *sp0, *sp1, temp;
145 const u_char **an, *t, **aj, **top[256];
153 simplesort(a, n, i, tr, endch);
158 /* Make character histogram. */
160 bmin = 255; /* First occupied bin, excluding eos. */
161 for (ak = a; ak < an;) {
163 if (++count[c] == 1 && c != endch) {
169 if (sp + nc > s + SIZE) { /* Get more stack. */
170 r_sort_a(a, n, i, tr, endch);
176 * Special case: if all strings have the same
177 * character at position i, move on to the next
180 if (nc == 1 && count[bmin] == n) {
182 nc = count[bmin] = 0;
187 * Set top[]; push incompletely sorted bins onto stack.
188 * top[] = pointers to last out-of-place element in bins.
189 * count[] = counts of elements in bins.
190 * Before permuting: top[c-1] + count[c] = top[c];
191 * during deal: top[c] counts down to top[c-1].
193 sp0 = sp1 = sp; /* Stack position of biggest bin. */
194 bigc = 2; /* Size of biggest bin. */
195 if (endch == 0) /* Special case: set top[eos]. */
196 top[0] = ak = a + count[0];
201 for (cp = count + bmin; nc > 0; cp++) {
202 while (*cp == 0) /* Find next non-empty pile. */
211 top[cp-count] = ak += *cp;
214 swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
217 * Permute misplacements home. Already home: everything
218 * before aj, and in bin[c], items from top[c] on.
220 * r = next element to put in place;
221 * ak = top[r[i]] = location to put the next element.
222 * aj = bottom of 1st disordered bin.
224 * Once the 1st disordered bin is done, ie. aj >= ak,
225 * aj<-aj + count[c] connects the bins in a linked list;
228 for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
229 for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
234 /* Stable sort, requiring additional memory. */
236 r_sort_b(a, ta, n, i, tr, endch)
237 const u_char **a, **ta;
242 static int count[256], nc, bmin;
244 const u_char **ak, **ai;
245 stack s[512], *sp, *sp0, *sp1, temp;
246 const u_char **top[256];
254 simplesort(a, n, i, tr, endch);
260 for (ak = a + n; --ak >= a;) {
262 if (++count[c] == 1 && c != endch) {
268 if (sp + nc > s + SIZE) {
269 r_sort_b(a, ta, n, i, tr, endch);
277 top[0] = ak = a + count[0];
284 for (cp = count + bmin; nc > 0; cp++) {
294 top[cp-count] = ak += c;
295 *cp = 0; /* Reset count[]. */
298 swap(*sp0, *sp1, temp);
300 for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
302 for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
303 *--top[tr[(*ak)[i]]] = *ak;
308 simplesort(a, n, b, tr, endch) /* insertion sort */
315 const u_char **ak, **ai, *s, *t;
317 for (ak = a+1; --n >= 1; ak++)
318 for (ai = ak; ai > a; ai--) {
319 for (s = ai[0] + b, t = ai[-1] + b;
320 (ch = tr[*s]) != endch; s++, t++)
325 swap(ai[0], ai[-1], s);