xref: /freebsd/lib/libc/stdlib/radixsort.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
158f0484fSRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1990, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
858f0484fSRodney W. Grimes  * Peter McIlroy and by Dan Bernstein at New York University,
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18580b4d18SEd Maste  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes /*
3658f0484fSRodney W. Grimes  * Radixsort routines.
3758f0484fSRodney W. Grimes  *
3858f0484fSRodney W. Grimes  * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
3958f0484fSRodney W. Grimes  * Use radixsort(a, n, trace, endchar) for this case.
4058f0484fSRodney W. Grimes  *
4158f0484fSRodney W. Grimes  * For stable sorting (using N extra pointers) use sradixsort(), which calls
4258f0484fSRodney W. Grimes  * r_sort_b().
4358f0484fSRodney W. Grimes  *
4458f0484fSRodney W. Grimes  * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
4558f0484fSRodney W. Grimes  * "Engineering Radix Sort".
4658f0484fSRodney W. Grimes  */
4758f0484fSRodney W. Grimes 
4858f0484fSRodney W. Grimes #include <sys/types.h>
4958f0484fSRodney W. Grimes #include <stdlib.h>
5058f0484fSRodney W. Grimes #include <stddef.h>
5158f0484fSRodney W. Grimes #include <errno.h>
5258f0484fSRodney W. Grimes 
5358f0484fSRodney W. Grimes typedef struct {
5458f0484fSRodney W. Grimes 	const u_char **sa;
5558f0484fSRodney W. Grimes 	int sn, si;
5658f0484fSRodney W. Grimes } stack;
5758f0484fSRodney W. Grimes 
5858f0484fSRodney W. Grimes static inline void simplesort
59c05ac53bSDavid E. O'Brien (const u_char **, int, int, const u_char *, u_int);
60c05ac53bSDavid E. O'Brien static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
611372519bSDavid E. O'Brien static void r_sort_b(const u_char **, const u_char **, int, int,
621372519bSDavid E. O'Brien     const u_char *, u_int);
6358f0484fSRodney W. Grimes 
6458f0484fSRodney W. Grimes #define	THRESHOLD	20		/* Divert to simplesort(). */
6558f0484fSRodney W. Grimes #define	SIZE		512		/* Default stack size. */
6658f0484fSRodney W. Grimes 
6758f0484fSRodney W. Grimes #define SETUP {								\
6858f0484fSRodney W. Grimes 	if (tab == NULL) {						\
6958f0484fSRodney W. Grimes 		tr = tr0;						\
7058f0484fSRodney W. Grimes 		for (c = 0; c < endch; c++)				\
7158f0484fSRodney W. Grimes 			tr0[c] = c + 1;					\
7258f0484fSRodney W. Grimes 		tr0[c] = 0;						\
7358f0484fSRodney W. Grimes 		for (c++; c < 256; c++)					\
7458f0484fSRodney W. Grimes 			tr0[c] = c;					\
7558f0484fSRodney W. Grimes 		endch = 0;						\
7658f0484fSRodney W. Grimes 	} else {							\
7758f0484fSRodney W. Grimes 		endch = tab[endch];					\
7858f0484fSRodney W. Grimes 		tr = tab;						\
7958f0484fSRodney W. Grimes 		if (endch != 0 && endch != 255) {			\
8058f0484fSRodney W. Grimes 			errno = EINVAL;					\
8158f0484fSRodney W. Grimes 			return (-1);					\
8258f0484fSRodney W. Grimes 		}							\
8358f0484fSRodney W. Grimes 	}								\
8458f0484fSRodney W. Grimes }
8558f0484fSRodney W. Grimes 
8658f0484fSRodney W. Grimes int
radixsort(const u_char ** a,int n,const u_char * tab,u_int endch)8776470dd5SCraig Rodrigues radixsort(const u_char **a, int n, const u_char *tab, u_int endch)
8858f0484fSRodney W. Grimes {
8958f0484fSRodney W. Grimes 	const u_char *tr;
9058f0484fSRodney W. Grimes 	int c;
9158f0484fSRodney W. Grimes 	u_char tr0[256];
9258f0484fSRodney W. Grimes 
9358f0484fSRodney W. Grimes 	SETUP;
9458f0484fSRodney W. Grimes 	r_sort_a(a, n, 0, tr, endch);
9558f0484fSRodney W. Grimes 	return (0);
9658f0484fSRodney W. Grimes }
9758f0484fSRodney W. Grimes 
9858f0484fSRodney W. Grimes int
sradixsort(const u_char ** a,int n,const u_char * tab,u_int endch)9976470dd5SCraig Rodrigues sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
10058f0484fSRodney W. Grimes {
10158f0484fSRodney W. Grimes 	const u_char *tr, **ta;
10258f0484fSRodney W. Grimes 	int c;
10358f0484fSRodney W. Grimes 	u_char tr0[256];
10458f0484fSRodney W. Grimes 
10558f0484fSRodney W. Grimes 	SETUP;
10658f0484fSRodney W. Grimes 	if (n < THRESHOLD)
10758f0484fSRodney W. Grimes 		simplesort(a, n, 0, tr, endch);
10858f0484fSRodney W. Grimes 	else {
10958f0484fSRodney W. Grimes 		if ((ta = malloc(n * sizeof(a))) == NULL)
11058f0484fSRodney W. Grimes 			return (-1);
11158f0484fSRodney W. Grimes 		r_sort_b(a, ta, n, 0, tr, endch);
11258f0484fSRodney W. Grimes 		free(ta);
11358f0484fSRodney W. Grimes 	}
11458f0484fSRodney W. Grimes 	return (0);
11558f0484fSRodney W. Grimes }
11658f0484fSRodney W. Grimes 
11758f0484fSRodney W. Grimes #define empty(s)	(s >= sp)
11858f0484fSRodney W. Grimes #define pop(a, n, i)	a = (--sp)->sa, n = sp->sn, i = sp->si
11958f0484fSRodney W. Grimes #define push(a, n, i)	sp->sa = a, sp->sn = n, (sp++)->si = i
12058f0484fSRodney W. Grimes #define swap(a, b, t)	t = a, a = b, b = t
12158f0484fSRodney W. Grimes 
12258f0484fSRodney W. Grimes /* Unstable, in-place sort. */
1234381233dSPeter Wemm static void
r_sort_a(const u_char ** a,int n,int i,const u_char * tr,u_int endch)12476470dd5SCraig Rodrigues r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch)
12558f0484fSRodney W. Grimes {
12658f0484fSRodney W. Grimes 	static int count[256], nc, bmin;
1278fb3f3f6SDavid E. O'Brien 	int c;
1288fb3f3f6SDavid E. O'Brien 	const u_char **ak, *r;
12958f0484fSRodney W. Grimes 	stack s[SIZE], *sp, *sp0, *sp1, temp;
13058f0484fSRodney W. Grimes 	int *cp, bigc;
13158f0484fSRodney W. Grimes 	const u_char **an, *t, **aj, **top[256];
13258f0484fSRodney W. Grimes 
13358f0484fSRodney W. Grimes 	/* Set up stack. */
13458f0484fSRodney W. Grimes 	sp = s;
13558f0484fSRodney W. Grimes 	push(a, n, i);
13658f0484fSRodney W. Grimes 	while (!empty(s)) {
13758f0484fSRodney W. Grimes 		pop(a, n, i);
13858f0484fSRodney W. Grimes 		if (n < THRESHOLD) {
13958f0484fSRodney W. Grimes 			simplesort(a, n, i, tr, endch);
14058f0484fSRodney W. Grimes 			continue;
14158f0484fSRodney W. Grimes 		}
14258f0484fSRodney W. Grimes 		an = a + n;
14358f0484fSRodney W. Grimes 
14458f0484fSRodney W. Grimes 		/* Make character histogram. */
14558f0484fSRodney W. Grimes 		if (nc == 0) {
14658f0484fSRodney W. Grimes 			bmin = 255;	/* First occupied bin, excluding eos. */
14758f0484fSRodney W. Grimes 			for (ak = a; ak < an;) {
14858f0484fSRodney W. Grimes 				c = tr[(*ak++)[i]];
14958f0484fSRodney W. Grimes 				if (++count[c] == 1 && c != endch) {
15058f0484fSRodney W. Grimes 					if (c < bmin)
15158f0484fSRodney W. Grimes 						bmin = c;
15258f0484fSRodney W. Grimes 					nc++;
15358f0484fSRodney W. Grimes 				}
15458f0484fSRodney W. Grimes 			}
15558f0484fSRodney W. Grimes 			if (sp + nc > s + SIZE) {	/* Get more stack. */
15658f0484fSRodney W. Grimes 				r_sort_a(a, n, i, tr, endch);
15758f0484fSRodney W. Grimes 				continue;
15858f0484fSRodney W. Grimes 			}
15958f0484fSRodney W. Grimes 		}
16058f0484fSRodney W. Grimes 
16158f0484fSRodney W. Grimes 		/*
162669073a7STim Kientzle 		 * Special case: if all strings have the same
163669073a7STim Kientzle 		 * character at position i, move on to the next
164669073a7STim Kientzle 		 * character.
165669073a7STim Kientzle 		 */
166669073a7STim Kientzle 		if (nc == 1 && count[bmin] == n) {
167669073a7STim Kientzle 			push(a, n, i+1);
168669073a7STim Kientzle 			nc = count[bmin] = 0;
169669073a7STim Kientzle 			continue;
170669073a7STim Kientzle 		}
171669073a7STim Kientzle 
172669073a7STim Kientzle 		/*
17358f0484fSRodney W. Grimes 		 * Set top[]; push incompletely sorted bins onto stack.
17458f0484fSRodney W. Grimes 		 * top[] = pointers to last out-of-place element in bins.
17558f0484fSRodney W. Grimes 		 * count[] = counts of elements in bins.
17658f0484fSRodney W. Grimes 		 * Before permuting: top[c-1] + count[c] = top[c];
17758f0484fSRodney W. Grimes 		 * during deal: top[c] counts down to top[c-1].
17858f0484fSRodney W. Grimes 		 */
17958f0484fSRodney W. Grimes 		sp0 = sp1 = sp;		/* Stack position of biggest bin. */
18058f0484fSRodney W. Grimes 		bigc = 2;		/* Size of biggest bin. */
18158f0484fSRodney W. Grimes 		if (endch == 0)		/* Special case: set top[eos]. */
18258f0484fSRodney W. Grimes 			top[0] = ak = a + count[0];
18358f0484fSRodney W. Grimes 		else {
18458f0484fSRodney W. Grimes 			ak = a;
18558f0484fSRodney W. Grimes 			top[255] = an;
18658f0484fSRodney W. Grimes 		}
18758f0484fSRodney W. Grimes 		for (cp = count + bmin; nc > 0; cp++) {
18858f0484fSRodney W. Grimes 			while (*cp == 0)	/* Find next non-empty pile. */
18958f0484fSRodney W. Grimes 				cp++;
19058f0484fSRodney W. Grimes 			if (*cp > 1) {
19158f0484fSRodney W. Grimes 				if (*cp > bigc) {
19258f0484fSRodney W. Grimes 					bigc = *cp;
19358f0484fSRodney W. Grimes 					sp1 = sp;
19458f0484fSRodney W. Grimes 				}
19558f0484fSRodney W. Grimes 				push(ak, *cp, i+1);
19658f0484fSRodney W. Grimes 			}
19758f0484fSRodney W. Grimes 			top[cp-count] = ak += *cp;
19858f0484fSRodney W. Grimes 			nc--;
19958f0484fSRodney W. Grimes 		}
20058f0484fSRodney W. Grimes 		swap(*sp0, *sp1, temp);	/* Play it safe -- biggest bin last. */
20158f0484fSRodney W. Grimes 
20258f0484fSRodney W. Grimes 		/*
20358f0484fSRodney W. Grimes 		 * Permute misplacements home.  Already home: everything
20458f0484fSRodney W. Grimes 		 * before aj, and in bin[c], items from top[c] on.
20558f0484fSRodney W. Grimes 		 * Inner loop:
20658f0484fSRodney W. Grimes 		 *	r = next element to put in place;
20758f0484fSRodney W. Grimes 		 *	ak = top[r[i]] = location to put the next element.
20858f0484fSRodney W. Grimes 		 *	aj = bottom of 1st disordered bin.
20958f0484fSRodney W. Grimes 		 * Outer loop:
21058f0484fSRodney W. Grimes 		 *	Once the 1st disordered bin is done, ie. aj >= ak,
21158f0484fSRodney W. Grimes 		 *	aj<-aj + count[c] connects the bins in a linked list;
21258f0484fSRodney W. Grimes 		 *	reset count[c].
21358f0484fSRodney W. Grimes 		 */
21458f0484fSRodney W. Grimes 		for (aj = a; aj < an;  *aj = r, aj += count[c], count[c] = 0)
21558f0484fSRodney W. Grimes 			for (r = *aj;  aj < (ak = --top[c = tr[r[i]]]);)
21658f0484fSRodney W. Grimes 				swap(*ak, r, t);
21758f0484fSRodney W. Grimes 	}
21858f0484fSRodney W. Grimes }
21958f0484fSRodney W. Grimes 
22058f0484fSRodney W. Grimes /* Stable sort, requiring additional memory. */
2214381233dSPeter Wemm static void
r_sort_b(const u_char ** a,const u_char ** ta,int n,int i,const u_char * tr,u_int endch)22276470dd5SCraig Rodrigues r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
22376470dd5SCraig Rodrigues     u_int endch)
22458f0484fSRodney W. Grimes {
22558f0484fSRodney W. Grimes 	static int count[256], nc, bmin;
2268fb3f3f6SDavid E. O'Brien 	int c;
2278fb3f3f6SDavid E. O'Brien 	const u_char **ak, **ai;
22858f0484fSRodney W. Grimes 	stack s[512], *sp, *sp0, *sp1, temp;
22958f0484fSRodney W. Grimes 	const u_char **top[256];
23058f0484fSRodney W. Grimes 	int *cp, bigc;
23158f0484fSRodney W. Grimes 
23258f0484fSRodney W. Grimes 	sp = s;
23358f0484fSRodney W. Grimes 	push(a, n, i);
23458f0484fSRodney W. Grimes 	while (!empty(s)) {
23558f0484fSRodney W. Grimes 		pop(a, n, i);
23658f0484fSRodney W. Grimes 		if (n < THRESHOLD) {
23758f0484fSRodney W. Grimes 			simplesort(a, n, i, tr, endch);
23858f0484fSRodney W. Grimes 			continue;
23958f0484fSRodney W. Grimes 		}
24058f0484fSRodney W. Grimes 
24158f0484fSRodney W. Grimes 		if (nc == 0) {
24258f0484fSRodney W. Grimes 			bmin = 255;
24358f0484fSRodney W. Grimes 			for (ak = a + n; --ak >= a;) {
24458f0484fSRodney W. Grimes 				c = tr[(*ak)[i]];
24558f0484fSRodney W. Grimes 				if (++count[c] == 1 && c != endch) {
24658f0484fSRodney W. Grimes 					if (c < bmin)
24758f0484fSRodney W. Grimes 						bmin = c;
24858f0484fSRodney W. Grimes 					nc++;
24958f0484fSRodney W. Grimes 				}
25058f0484fSRodney W. Grimes 			}
25158f0484fSRodney W. Grimes 			if (sp + nc > s + SIZE) {
25258f0484fSRodney W. Grimes 				r_sort_b(a, ta, n, i, tr, endch);
25358f0484fSRodney W. Grimes 				continue;
25458f0484fSRodney W. Grimes 			}
25558f0484fSRodney W. Grimes 		}
25658f0484fSRodney W. Grimes 
25758f0484fSRodney W. Grimes 		sp0 = sp1 = sp;
25858f0484fSRodney W. Grimes 		bigc = 2;
25958f0484fSRodney W. Grimes 		if (endch == 0) {
26058f0484fSRodney W. Grimes 			top[0] = ak = a + count[0];
26158f0484fSRodney W. Grimes 			count[0] = 0;
26258f0484fSRodney W. Grimes 		} else {
26358f0484fSRodney W. Grimes 			ak = a;
26458f0484fSRodney W. Grimes 			top[255] = a + n;
26558f0484fSRodney W. Grimes 			count[255] = 0;
26658f0484fSRodney W. Grimes 		}
26758f0484fSRodney W. Grimes 		for (cp = count + bmin; nc > 0; cp++) {
26858f0484fSRodney W. Grimes 			while (*cp == 0)
26958f0484fSRodney W. Grimes 				cp++;
27058f0484fSRodney W. Grimes 			if ((c = *cp) > 1) {
27158f0484fSRodney W. Grimes 				if (c > bigc) {
27258f0484fSRodney W. Grimes 					bigc = c;
27358f0484fSRodney W. Grimes 					sp1 = sp;
27458f0484fSRodney W. Grimes 				}
27558f0484fSRodney W. Grimes 				push(ak, c, i+1);
27658f0484fSRodney W. Grimes 			}
27758f0484fSRodney W. Grimes 			top[cp-count] = ak += c;
27858f0484fSRodney W. Grimes 			*cp = 0;			/* Reset count[]. */
27958f0484fSRodney W. Grimes 			nc--;
28058f0484fSRodney W. Grimes 		}
28158f0484fSRodney W. Grimes 		swap(*sp0, *sp1, temp);
28258f0484fSRodney W. Grimes 
28358f0484fSRodney W. Grimes 		for (ak = ta + n, ai = a+n; ak > ta;)	/* Copy to temp. */
28458f0484fSRodney W. Grimes 			*--ak = *--ai;
28558f0484fSRodney W. Grimes 		for (ak = ta+n; --ak >= ta;)		/* Deal to piles. */
28658f0484fSRodney W. Grimes 			*--top[tr[(*ak)[i]]] = *ak;
28758f0484fSRodney W. Grimes 	}
28858f0484fSRodney W. Grimes }
28958f0484fSRodney W. Grimes 
29076470dd5SCraig Rodrigues /* insertion sort */
29158f0484fSRodney W. Grimes static inline void
simplesort(const u_char ** a,int n,int b,const u_char * tr,u_int endch)29276470dd5SCraig Rodrigues simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
29358f0484fSRodney W. Grimes {
2948fb3f3f6SDavid E. O'Brien 	u_char ch;
29558f0484fSRodney W. Grimes 	const u_char  **ak, **ai, *s, *t;
29658f0484fSRodney W. Grimes 
29758f0484fSRodney W. Grimes 	for (ak = a+1; --n >= 1; ak++)
29858f0484fSRodney W. Grimes 		for (ai = ak; ai > a; ai--) {
29958f0484fSRodney W. Grimes 			for (s = ai[0] + b, t = ai[-1] + b;
30058f0484fSRodney W. Grimes 			    (ch = tr[*s]) != endch; s++, t++)
30158f0484fSRodney W. Grimes 				if (ch != tr[*t])
30258f0484fSRodney W. Grimes 					break;
30358f0484fSRodney W. Grimes 			if (ch >= tr[*t])
30458f0484fSRodney W. Grimes 				break;
30558f0484fSRodney W. Grimes 			swap(ai[0], ai[-1], s);
30658f0484fSRodney W. Grimes 		}
30758f0484fSRodney W. Grimes }
308