radixsort.c revision 1.1.1.3 1 1.1 cgd /*-
2 1.1.1.2 mycroft * Copyright (c) 1990, 1993
3 1.1.1.2 mycroft * The Regents of the University of California. All rights reserved.
4 1.1.1.2 mycroft *
5 1.1.1.2 mycroft * This code is derived from software contributed to Berkeley by
6 1.1.1.2 mycroft * Peter McIlroy and by Dan Bernstein at New York University,
7 1.1 cgd *
8 1.1 cgd * Redistribution and use in source and binary forms, with or without
9 1.1 cgd * modification, are permitted provided that the following conditions
10 1.1 cgd * are met:
11 1.1 cgd * 1. Redistributions of source code must retain the above copyright
12 1.1 cgd * notice, this list of conditions and the following disclaimer.
13 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
14 1.1 cgd * notice, this list of conditions and the following disclaimer in the
15 1.1 cgd * documentation and/or other materials provided with the distribution.
16 1.1 cgd * 3. All advertising materials mentioning features or use of this software
17 1.1 cgd * must display the following acknowledgement:
18 1.1 cgd * This product includes software developed by the University of
19 1.1 cgd * California, Berkeley and its contributors.
20 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
21 1.1 cgd * may be used to endorse or promote products derived from this software
22 1.1 cgd * without specific prior written permission.
23 1.1 cgd *
24 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 cgd * SUCH DAMAGE.
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint)
38 1.1.1.3 perry static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
39 1.1 cgd #endif /* LIBC_SCCS and not lint */
40 1.1 cgd
41 1.1 cgd /*
42 1.1.1.2 mycroft * Radixsort routines.
43 1.1.1.2 mycroft *
44 1.1.1.2 mycroft * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
45 1.1.1.2 mycroft * Use radixsort(a, n, trace, endchar) for this case.
46 1.1.1.2 mycroft *
47 1.1.1.2 mycroft * For stable sorting (using N extra pointers) use sradixsort(), which calls
48 1.1.1.2 mycroft * r_sort_b().
49 1.1.1.2 mycroft *
50 1.1.1.2 mycroft * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
51 1.1.1.2 mycroft * "Engineering Radix Sort".
52 1.1 cgd */
53 1.1 cgd
54 1.1.1.2 mycroft #include <sys/types.h>
55 1.1.1.2 mycroft #include <stdlib.h>
56 1.1.1.2 mycroft #include <stddef.h>
57 1.1.1.2 mycroft #include <errno.h>
58 1.1 cgd
59 1.1.1.2 mycroft typedef struct {
60 1.1.1.2 mycroft const u_char **sa;
61 1.1.1.2 mycroft int sn, si;
62 1.1.1.2 mycroft } stack;
63 1.1.1.2 mycroft
64 1.1.1.2 mycroft static inline void simplesort
65 1.1.1.2 mycroft __P((const u_char **, int, int, const u_char *, u_int));
66 1.1.1.2 mycroft static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int));
67 1.1.1.2 mycroft static void r_sort_b __P((const u_char **,
68 1.1.1.2 mycroft const u_char **, int, int, const u_char *, u_int));
69 1.1.1.2 mycroft
70 1.1.1.2 mycroft #define THRESHOLD 20 /* Divert to simplesort(). */
71 1.1.1.2 mycroft #define SIZE 512 /* Default stack size. */
72 1.1.1.2 mycroft
73 1.1.1.2 mycroft #define SETUP { \
74 1.1.1.2 mycroft if (tab == NULL) { \
75 1.1.1.2 mycroft tr = tr0; \
76 1.1.1.2 mycroft for (c = 0; c < endch; c++) \
77 1.1.1.2 mycroft tr0[c] = c + 1; \
78 1.1.1.2 mycroft tr0[c] = 0; \
79 1.1.1.2 mycroft for (c++; c < 256; c++) \
80 1.1.1.2 mycroft tr0[c] = c; \
81 1.1.1.2 mycroft endch = 0; \
82 1.1.1.2 mycroft } else { \
83 1.1.1.2 mycroft endch = tab[endch]; \
84 1.1.1.2 mycroft tr = tab; \
85 1.1.1.2 mycroft if (endch != 0 && endch != 255) { \
86 1.1.1.2 mycroft errno = EINVAL; \
87 1.1.1.2 mycroft return (-1); \
88 1.1.1.2 mycroft } \
89 1.1.1.2 mycroft } \
90 1.1 cgd }
91 1.1.1.2 mycroft
92 1.1.1.2 mycroft int
93 1.1.1.2 mycroft radixsort(a, n, tab, endch)
94 1.1.1.2 mycroft const u_char **a, *tab;
95 1.1.1.2 mycroft int n;
96 1.1.1.2 mycroft u_int endch;
97 1.1.1.2 mycroft {
98 1.1.1.2 mycroft const u_char *tr;
99 1.1.1.2 mycroft int c;
100 1.1.1.2 mycroft u_char tr0[256];
101 1.1.1.2 mycroft
102 1.1.1.2 mycroft SETUP;
103 1.1.1.2 mycroft r_sort_a(a, n, 0, tr, endch);
104 1.1.1.2 mycroft return (0);
105 1.1 cgd }
106 1.1 cgd
107 1.1 cgd int
108 1.1.1.2 mycroft sradixsort(a, n, tab, endch)
109 1.1.1.2 mycroft const u_char **a, *tab;
110 1.1.1.2 mycroft int n;
111 1.1.1.2 mycroft u_int endch;
112 1.1 cgd {
113 1.1.1.2 mycroft const u_char *tr, **ta;
114 1.1.1.2 mycroft int c;
115 1.1.1.2 mycroft u_char tr0[256];
116 1.1.1.2 mycroft
117 1.1.1.2 mycroft SETUP;
118 1.1.1.2 mycroft if (n < THRESHOLD)
119 1.1.1.2 mycroft simplesort(a, n, 0, tr, endch);
120 1.1 cgd else {
121 1.1.1.2 mycroft if ((ta = malloc(n * sizeof(a))) == NULL)
122 1.1.1.2 mycroft return (-1);
123 1.1.1.2 mycroft r_sort_b(a, ta, n, 0, tr, endch);
124 1.1.1.2 mycroft free(ta);
125 1.1 cgd }
126 1.1.1.2 mycroft return (0);
127 1.1.1.2 mycroft }
128 1.1 cgd
129 1.1.1.2 mycroft #define empty(s) (s >= sp)
130 1.1.1.2 mycroft #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
131 1.1.1.2 mycroft #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
132 1.1.1.2 mycroft #define swap(a, b, t) t = a, a = b, b = t
133 1.1.1.2 mycroft
134 1.1.1.2 mycroft /* Unstable, in-place sort. */
135 1.1.1.3 perry static void
136 1.1.1.2 mycroft r_sort_a(a, n, i, tr, endch)
137 1.1.1.2 mycroft const u_char **a;
138 1.1.1.2 mycroft int n, i;
139 1.1.1.2 mycroft const u_char *tr;
140 1.1.1.2 mycroft u_int endch;
141 1.1.1.2 mycroft {
142 1.1.1.2 mycroft static int count[256], nc, bmin;
143 1.1.1.2 mycroft register int c;
144 1.1.1.2 mycroft register const u_char **ak, *r;
145 1.1.1.2 mycroft stack s[SIZE], *sp, *sp0, *sp1, temp;
146 1.1.1.2 mycroft int *cp, bigc;
147 1.1.1.2 mycroft const u_char **an, *t, **aj, **top[256];
148 1.1.1.2 mycroft
149 1.1.1.2 mycroft /* Set up stack. */
150 1.1.1.2 mycroft sp = s;
151 1.1.1.2 mycroft push(a, n, i);
152 1.1.1.2 mycroft while (!empty(s)) {
153 1.1.1.2 mycroft pop(a, n, i);
154 1.1.1.2 mycroft if (n < THRESHOLD) {
155 1.1.1.2 mycroft simplesort(a, n, i, tr, endch);
156 1.1.1.2 mycroft continue;
157 1.1.1.2 mycroft }
158 1.1.1.2 mycroft an = a + n;
159 1.1 cgd
160 1.1.1.2 mycroft /* Make character histogram. */
161 1.1.1.2 mycroft if (nc == 0) {
162 1.1.1.2 mycroft bmin = 255; /* First occupied bin, excluding eos. */
163 1.1.1.2 mycroft for (ak = a; ak < an;) {
164 1.1.1.2 mycroft c = tr[(*ak++)[i]];
165 1.1.1.2 mycroft if (++count[c] == 1 && c != endch) {
166 1.1.1.2 mycroft if (c < bmin)
167 1.1.1.2 mycroft bmin = c;
168 1.1.1.2 mycroft nc++;
169 1.1.1.2 mycroft }
170 1.1.1.2 mycroft }
171 1.1.1.2 mycroft if (sp + nc > s + SIZE) { /* Get more stack. */
172 1.1.1.2 mycroft r_sort_a(a, n, i, tr, endch);
173 1.1.1.2 mycroft continue;
174 1.1.1.2 mycroft }
175 1.1.1.2 mycroft }
176 1.1 cgd
177 1.1 cgd /*
178 1.1.1.2 mycroft * Set top[]; push incompletely sorted bins onto stack.
179 1.1.1.2 mycroft * top[] = pointers to last out-of-place element in bins.
180 1.1.1.2 mycroft * count[] = counts of elements in bins.
181 1.1.1.2 mycroft * Before permuting: top[c-1] + count[c] = top[c];
182 1.1.1.2 mycroft * during deal: top[c] counts down to top[c-1].
183 1.1 cgd */
184 1.1.1.2 mycroft sp0 = sp1 = sp; /* Stack position of biggest bin. */
185 1.1.1.2 mycroft bigc = 2; /* Size of biggest bin. */
186 1.1.1.2 mycroft if (endch == 0) /* Special case: set top[eos]. */
187 1.1.1.2 mycroft top[0] = ak = a + count[0];
188 1.1.1.2 mycroft else {
189 1.1.1.2 mycroft ak = a;
190 1.1.1.2 mycroft top[255] = an;
191 1.1.1.2 mycroft }
192 1.1.1.2 mycroft for (cp = count + bmin; nc > 0; cp++) {
193 1.1.1.2 mycroft while (*cp == 0) /* Find next non-empty pile. */
194 1.1.1.2 mycroft cp++;
195 1.1.1.2 mycroft if (*cp > 1) {
196 1.1.1.2 mycroft if (*cp > bigc) {
197 1.1.1.2 mycroft bigc = *cp;
198 1.1.1.2 mycroft sp1 = sp;
199 1.1.1.2 mycroft }
200 1.1.1.2 mycroft push(ak, *cp, i+1);
201 1.1 cgd }
202 1.1.1.2 mycroft top[cp-count] = ak += *cp;
203 1.1.1.2 mycroft nc--;
204 1.1 cgd }
205 1.1.1.2 mycroft swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
206 1.1 cgd
207 1.1 cgd /*
208 1.1.1.2 mycroft * Permute misplacements home. Already home: everything
209 1.1.1.2 mycroft * before aj, and in bin[c], items from top[c] on.
210 1.1.1.2 mycroft * Inner loop:
211 1.1.1.2 mycroft * r = next element to put in place;
212 1.1.1.2 mycroft * ak = top[r[i]] = location to put the next element.
213 1.1.1.2 mycroft * aj = bottom of 1st disordered bin.
214 1.1.1.2 mycroft * Outer loop:
215 1.1.1.2 mycroft * Once the 1st disordered bin is done, ie. aj >= ak,
216 1.1.1.2 mycroft * aj<-aj + count[c] connects the bins in a linked list;
217 1.1.1.2 mycroft * reset count[c].
218 1.1 cgd */
219 1.1.1.2 mycroft for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
220 1.1.1.2 mycroft for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
221 1.1.1.2 mycroft swap(*ak, r, t);
222 1.1.1.2 mycroft }
223 1.1.1.2 mycroft }
224 1.1 cgd
225 1.1.1.2 mycroft /* Stable sort, requiring additional memory. */
226 1.1.1.3 perry static void
227 1.1.1.2 mycroft r_sort_b(a, ta, n, i, tr, endch)
228 1.1.1.2 mycroft const u_char **a, **ta;
229 1.1.1.2 mycroft int n, i;
230 1.1.1.2 mycroft const u_char *tr;
231 1.1.1.2 mycroft u_int endch;
232 1.1.1.2 mycroft {
233 1.1.1.2 mycroft static int count[256], nc, bmin;
234 1.1.1.2 mycroft register int c;
235 1.1.1.2 mycroft register const u_char **ak, **ai;
236 1.1.1.2 mycroft stack s[512], *sp, *sp0, *sp1, temp;
237 1.1.1.2 mycroft const u_char **top[256];
238 1.1.1.2 mycroft int *cp, bigc;
239 1.1.1.2 mycroft
240 1.1.1.2 mycroft sp = s;
241 1.1.1.2 mycroft push(a, n, i);
242 1.1.1.2 mycroft while (!empty(s)) {
243 1.1.1.2 mycroft pop(a, n, i);
244 1.1.1.2 mycroft if (n < THRESHOLD) {
245 1.1.1.2 mycroft simplesort(a, n, i, tr, endch);
246 1.1.1.2 mycroft continue;
247 1.1.1.2 mycroft }
248 1.1 cgd
249 1.1.1.2 mycroft if (nc == 0) {
250 1.1.1.2 mycroft bmin = 255;
251 1.1.1.2 mycroft for (ak = a + n; --ak >= a;) {
252 1.1.1.2 mycroft c = tr[(*ak)[i]];
253 1.1.1.2 mycroft if (++count[c] == 1 && c != endch) {
254 1.1.1.2 mycroft if (c < bmin)
255 1.1.1.2 mycroft bmin = c;
256 1.1.1.2 mycroft nc++;
257 1.1.1.2 mycroft }
258 1.1.1.2 mycroft }
259 1.1.1.2 mycroft if (sp + nc > s + SIZE) {
260 1.1.1.2 mycroft r_sort_b(a, ta, n, i, tr, endch);
261 1.1 cgd continue;
262 1.1.1.2 mycroft }
263 1.1 cgd }
264 1.1.1.2 mycroft
265 1.1.1.2 mycroft sp0 = sp1 = sp;
266 1.1.1.2 mycroft bigc = 2;
267 1.1.1.2 mycroft if (endch == 0) {
268 1.1.1.2 mycroft top[0] = ak = a + count[0];
269 1.1.1.2 mycroft count[0] = 0;
270 1.1.1.2 mycroft } else {
271 1.1.1.2 mycroft ak = a;
272 1.1.1.2 mycroft top[255] = a + n;
273 1.1.1.2 mycroft count[255] = 0;
274 1.1 cgd }
275 1.1.1.2 mycroft for (cp = count + bmin; nc > 0; cp++) {
276 1.1.1.2 mycroft while (*cp == 0)
277 1.1.1.2 mycroft cp++;
278 1.1.1.2 mycroft if ((c = *cp) > 1) {
279 1.1.1.2 mycroft if (c > bigc) {
280 1.1.1.2 mycroft bigc = c;
281 1.1.1.2 mycroft sp1 = sp;
282 1.1.1.2 mycroft }
283 1.1.1.2 mycroft push(ak, c, i+1);
284 1.1.1.2 mycroft }
285 1.1.1.2 mycroft top[cp-count] = ak += c;
286 1.1.1.2 mycroft *cp = 0; /* Reset count[]. */
287 1.1.1.2 mycroft nc--;
288 1.1.1.2 mycroft }
289 1.1.1.2 mycroft swap(*sp0, *sp1, temp);
290 1.1 cgd
291 1.1.1.2 mycroft for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
292 1.1.1.2 mycroft *--ak = *--ai;
293 1.1.1.2 mycroft for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
294 1.1.1.2 mycroft *--top[tr[(*ak)[i]]] = *ak;
295 1.1.1.2 mycroft }
296 1.1 cgd }
297 1.1.1.2 mycroft
298 1.1.1.2 mycroft static inline void
299 1.1.1.2 mycroft simplesort(a, n, b, tr, endch) /* insertion sort */
300 1.1.1.2 mycroft register const u_char **a;
301 1.1.1.2 mycroft int n, b;
302 1.1.1.2 mycroft register const u_char *tr;
303 1.1.1.2 mycroft u_int endch;
304 1.1 cgd {
305 1.1.1.2 mycroft register u_char ch;
306 1.1.1.2 mycroft const u_char **ak, **ai, *s, *t;
307 1.1 cgd
308 1.1.1.2 mycroft for (ak = a+1; --n >= 1; ak++)
309 1.1.1.2 mycroft for (ai = ak; ai > a; ai--) {
310 1.1.1.2 mycroft for (s = ai[0] + b, t = ai[-1] + b;
311 1.1.1.2 mycroft (ch = tr[*s]) != endch; s++, t++)
312 1.1.1.2 mycroft if (ch != tr[*t])
313 1.1 cgd break;
314 1.1.1.2 mycroft if (ch >= tr[*t])
315 1.1.1.2 mycroft break;
316 1.1.1.2 mycroft swap(ai[0], ai[-1], s);
317 1.1.1.2 mycroft }
318 1.1 cgd }
319