1 /* $NetBSD: radixsort.c,v 1.18 2009/08/21 20:49:50 dsl Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Peter McIlroy and by Dan Bernstein at New York University,
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 #include <sys/cdefs.h>
36 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
40 __RCSID("$NetBSD: radixsort.c,v 1.18 2009/08/21 20:49:50 dsl Exp $");
42 #endif /* LIBC_SCCS and not lint */
47 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
48 * Use radixsort(a, n, trace, endchar) for this case.
50 * For stable sorting (using N extra pointers) use sradixsort(), which calls
53 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
54 * "Engineering Radix Sort".
57 #include "namespace.h"
58 #include <sys/types.h>
65 __weak_alias(radixsort
,_radixsort
)
66 __weak_alias(sradixsort
,_sradixsort
)
74 static inline void simplesort(const u_char
**, int, int, const u_char
*, u_int
);
75 static void r_sort_a(const u_char
**, int, int, const u_char
*, u_int
);
76 static void r_sort_b(const u_char
**,
77 const u_char
**, int, int, const u_char
*, u_int
);
79 #define THRESHOLD 20 /* Divert to simplesort(). */
80 #define SIZE 512 /* Default stack size. */
85 for (c = 0; c < endch; c++) \
88 for (c++; c < 256; c++) \
94 if (endch != 0 && endch != 255) { \
102 radixsort(const u_char
**a
, int n
, const u_char
*tab
, u_int endch
)
108 _DIAGASSERT(a
!= NULL
);
111 r_sort_a(a
, n
, 0, tr
, endch
);
116 sradixsort(const u_char
**a
, int n
, const u_char
*tab
, u_int endch
)
118 const u_char
*tr
, **ta
;
122 _DIAGASSERT(a
!= NULL
);
130 simplesort(a
, n
, 0, tr
, endch
);
132 if ((ta
= malloc(n
* sizeof(a
))) == NULL
)
134 r_sort_b(a
, ta
, n
, 0, tr
, endch
);
140 #define empty(s) (s >= sp)
141 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
142 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
143 #define swap(a, b, t) t = a, a = b, b = t
145 /* Unstable, in-place sort. */
147 r_sort_a(const u_char
**a
, int n
, int i
, const u_char
*tr
, u_int endch
)
149 static u_int count
[256], nc
, bmin
;
151 const u_char
**ak
, *r
;
152 stack s
[SIZE
], *sp
, *sp0
, *sp1
, temp
;
154 const u_char
**an
, *t
, **aj
, **top
[256];
156 _DIAGASSERT(a
!= NULL
);
157 _DIAGASSERT(tr
!= NULL
);
165 simplesort(a
, n
, i
, tr
, endch
);
170 /* Make character histogram. */
172 bmin
= 255; /* First occupied bin, excluding eos. */
173 for (ak
= a
; ak
< an
;) {
175 if (++count
[c
] == 1 && c
!= endch
) {
181 if (sp
+ nc
> s
+ SIZE
) { /* Get more stack. */
182 r_sort_a(a
, n
, i
, tr
, endch
);
188 * Set top[]; push incompletely sorted bins onto stack.
189 * top[] = pointers to last out-of-place element in bins.
190 * count[] = counts of elements in bins.
191 * Before permuting: top[c-1] + count[c] = top[c];
192 * during deal: top[c] counts down to top[c-1].
194 sp0
= sp1
= sp
; /* Stack position of biggest bin. */
195 bigc
= 2; /* Size of biggest bin. */
196 if (endch
== 0) /* Special case: set top[eos]. */
197 top
[0] = ak
= a
+ count
[0];
202 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
203 while (*cp
== 0) /* Find next non-empty pile. */
212 top
[cp
-count
] = ak
+= *cp
;
215 swap(*sp0
, *sp1
, temp
); /* Play it safe -- biggest bin last. */
218 * Permute misplacements home. Already home: everything
219 * before aj, and in bin[c], items from top[c] on.
221 * r = next element to put in place;
222 * ak = top[r[i]] = location to put the next element.
223 * aj = bottom of 1st disordered bin.
225 * Once the 1st disordered bin is done, ie. aj >= ak,
226 * aj<-aj + count[c] connects the bins in a linked list;
229 for (aj
= a
; aj
< an
; *aj
= r
, aj
+= count
[c
], count
[c
] = 0)
230 for (r
= *aj
; aj
< (ak
= --top
[c
= tr
[r
[i
]]]);)
235 /* Stable sort, requiring additional memory. */
237 r_sort_b(const u_char
**a
, const u_char
**ta
, int n
, int i
, const u_char
*tr
,
240 static u_int count
[256], nc
, bmin
;
242 const u_char
**ak
, **ai
;
243 stack s
[512], *sp
, *sp0
, *sp1
, temp
;
244 const u_char
**top
[256];
247 _DIAGASSERT(a
!= NULL
);
248 _DIAGASSERT(ta
!= NULL
);
249 _DIAGASSERT(tr
!= NULL
);
256 simplesort(a
, n
, i
, tr
, endch
);
262 for (ak
= a
+ n
; --ak
>= a
;) {
264 if (++count
[c
] == 1 && c
!= endch
) {
270 if (sp
+ nc
> s
+ SIZE
) {
271 r_sort_b(a
, ta
, n
, i
, tr
, endch
);
279 top
[0] = ak
= a
+ count
[0];
286 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
296 top
[cp
-count
] = ak
+= c
;
297 *cp
= 0; /* Reset count[]. */
300 swap(*sp0
, *sp1
, temp
);
302 for (ak
= ta
+ n
, ai
= a
+n
; ak
> ta
;) /* Copy to temp. */
304 for (ak
= ta
+n
; --ak
>= ta
;) /* Deal to piles. */
305 *--top
[tr
[(*ak
)[i
]]] = *ak
;
311 simplesort(const u_char
**a
, int n
, int b
, const u_char
*tr
, u_int endch
)
314 const u_char
**ak
, **ai
, *s
, *t
;
316 _DIAGASSERT(a
!= NULL
);
317 _DIAGASSERT(tr
!= NULL
);
319 for (ak
= a
+1; --n
>= 1; ak
++)
320 for (ai
= ak
; ai
> a
; ai
--) {
321 for (s
= ai
[0] + b
, t
= ai
[-1] + b
;
322 (ch
= tr
[*s
]) != endch
; s
++, t
++)
327 swap(ai
[0], ai
[-1], s
);