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 * 4. 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: src/lib/libc/stdlib/radixsort.c,v 1.8 2007/01/09 00:28:10 imp Exp $");
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>
63 static inline void simplesort
64 (const u_char
**, int, int, const u_char
*, u_int
) __attribute__((always_inline
));
65 static void r_sort_a(const u_char
**, int, int, const u_char
*, u_int
);
66 static void r_sort_b(const u_char
**, const u_char
**, int, int,
67 const u_char
*, u_int
);
69 static int *r_sort_a_count
;
70 static int *r_sort_b_count
;
72 static void r_sort_count_allocate(void);
73 static pthread_once_t r_sort_count_control
= PTHREAD_ONCE_INIT
;
75 #define THRESHOLD 20 /* Divert to simplesort(). */
76 #define SIZE 512 /* Default stack size. */
81 for (c = 0; c < endch; c++) \
84 for (c++; c < 256; c++) \
90 if (endch != 0 && endch != 255) { \
98 radixsort(a
, n
, tab
, endch
)
99 const u_char
**a
, *tab
;
108 r_sort_a(a
, n
, 0, tr
, endch
);
113 sradixsort(a
, n
, tab
, endch
)
114 const u_char
**a
, *tab
;
118 const u_char
*tr
, **ta
;
124 simplesort(a
, n
, 0, tr
, endch
);
126 if ((ta
= malloc(n
* sizeof(a
))) == NULL
)
128 r_sort_b(a
, ta
, n
, 0, tr
, endch
);
134 static void r_sort_count_allocate(void)
136 r_sort_a_count
= calloc(256, sizeof(int));
137 r_sort_b_count
= calloc(256, sizeof(int));
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(a
, n
, i
, tr
, endch
)
153 static int *count
, nc
, bmin
;
155 const u_char
**ak
, *r
;
156 stack s
[SIZE
], *sp
, *sp0
, *sp1
, temp
;
158 const u_char
**an
, *t
, **aj
, **top
[256];
160 if (pthread_once(&r_sort_count_control
, r_sort_count_allocate
)) {
164 count
= r_sort_a_count
;
172 simplesort(a
, n
, i
, tr
, endch
);
177 /* Make character histogram. */
179 bmin
= 255; /* First occupied bin, excluding eos. */
180 for (ak
= a
; ak
< an
;) {
182 if (++count
[c
] == 1 && c
!= endch
) {
188 if (sp
+ nc
> s
+ SIZE
) { /* Get more stack. */
189 r_sort_a(a
, n
, i
, tr
, endch
);
195 * Special case: if all strings have the same
196 * character at position i, move on to the next
199 if (nc
== 1 && count
[bmin
] == n
) {
201 nc
= count
[bmin
] = 0;
206 * Set top[]; push incompletely sorted bins onto stack.
207 * top[] = pointers to last out-of-place element in bins.
208 * count[] = counts of elements in bins.
209 * Before permuting: top[c-1] + count[c] = top[c];
210 * during deal: top[c] counts down to top[c-1].
212 sp0
= sp1
= sp
; /* Stack position of biggest bin. */
213 bigc
= 2; /* Size of biggest bin. */
214 if (endch
== 0) /* Special case: set top[eos]. */
215 top
[0] = ak
= a
+ count
[0];
220 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
221 while (*cp
== 0) /* Find next non-empty pile. */
230 top
[cp
-count
] = ak
+= *cp
;
233 swap(*sp0
, *sp1
, temp
); /* Play it safe -- biggest bin last. */
236 * Permute misplacements home. Already home: everything
237 * before aj, and in bin[c], items from top[c] on.
239 * r = next element to put in place;
240 * ak = top[r[i]] = location to put the next element.
241 * aj = bottom of 1st disordered bin.
243 * Once the 1st disordered bin is done, ie. aj >= ak,
244 * aj<-aj + count[c] connects the bins in a linked list;
247 for (aj
= a
; aj
< an
; *aj
= r
, aj
+= count
[c
], count
[c
] = 0)
248 for (r
= *aj
; aj
< (ak
= --top
[c
= tr
[r
[i
]]]);)
253 /* Stable sort, requiring additional memory. */
255 r_sort_b(a
, ta
, n
, i
, tr
, endch
)
256 const u_char
**a
, **ta
;
261 static int *count
, nc
, bmin
;
263 const u_char
**ak
, **ai
;
264 stack s
[512], *sp
, *sp0
, *sp1
, temp
;
265 const u_char
**top
[256];
268 if (pthread_once(&r_sort_count_control
, r_sort_count_allocate
)) {
272 count
= r_sort_b_count
;
279 simplesort(a
, n
, i
, tr
, endch
);
285 for (ak
= a
+ n
; --ak
>= a
;) {
287 if (++count
[c
] == 1 && c
!= endch
) {
293 if (sp
+ nc
> s
+ SIZE
) {
294 r_sort_b(a
, ta
, n
, i
, tr
, endch
);
302 top
[0] = ak
= a
+ count
[0];
309 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
319 top
[cp
-count
] = ak
+= c
;
320 *cp
= 0; /* Reset count[]. */
323 swap(*sp0
, *sp1
, temp
);
325 for (ak
= ta
+ n
, ai
= a
+n
; ak
> ta
;) /* Copy to temp. */
327 for (ak
= ta
+n
; --ak
>= ta
;) /* Deal to piles. */
328 *--top
[tr
[(*ak
)[i
]]] = *ak
;
333 simplesort(a
, n
, b
, tr
, endch
) /* insertion sort */
340 const u_char
**ak
, **ai
, *s
, *t
;
342 for (ak
= a
+1; --n
>= 1; ak
++)
343 for (ai
= ak
; ai
> a
; ai
--) {
344 for (s
= ai
[0] + b
, t
= ai
[-1] + b
;
345 (ch
= tr
[*s
]) != endch
; s
++, t
++)
350 swap(ai
[0], ai
[-1], s
);