xref: /csrg-svn/lib/libc/stdlib/radixsort.c (revision 45935)
145287Sbostic /*-
245287Sbostic  * Copyright (c) 1990 The Regents of the University of California.
345287Sbostic  * All rights reserved.
445287Sbostic  *
545287Sbostic  * %sccs.include.redist.c%
645287Sbostic  */
745287Sbostic 
845287Sbostic #if defined(LIBC_SCCS) && !defined(lint)
9*45935Sbostic static char sccsid[] = "@(#)radixsort.c	5.6 (Berkeley) 01/13/91";
1045287Sbostic #endif /* LIBC_SCCS and not lint */
1145287Sbostic 
1245287Sbostic #include <sys/types.h>
1345287Sbostic #include <limits.h>
1445287Sbostic #include <stdlib.h>
1545287Sbostic #include <stddef.h>
1645287Sbostic 
1745287Sbostic /*
1845287Sbostic  * __rspartition is the cutoff point for a further partitioning instead
1945287Sbostic  * of a shellsort.  If it changes check __rsshell_increments.  Both of
20*45935Sbostic  * these are exported, as the best values are data dependent.
2145287Sbostic  */
2245287Sbostic #define	NPARTITION	40
2345287Sbostic int __rspartition = NPARTITION;
2445287Sbostic int __rsshell_increments[] = { 4, 1, 0, 0, 0, 0, 0, 0 };
2545287Sbostic 
2645287Sbostic /*
2745345Sbostic  * Stackp points to context structures, where each structure schedules a
2845345Sbostic  * partitioning.  Radixsort exits when the stack is empty.
2945287Sbostic  *
3045346Sbostic  * If the buckets are placed on the stack randomly, the worst case is when
3145427Sbostic  * all the buckets but one contain (npartitions + 1) elements and the bucket
3245346Sbostic  * pushed on the stack last contains the rest of the elements.  In this case,
3345346Sbostic  * stack growth is bounded by:
3445345Sbostic  *
3545427Sbostic  *	limit = (nelements / (npartitions + 1)) - 1;
3645345Sbostic  *
3745427Sbostic  * This is a very large number, 52,377,648 for the maximum 32-bit signed int.
3845345Sbostic  *
3945427Sbostic  * By forcing the largest bucket to be pushed on the stack first, the worst
4045427Sbostic  * case is when all but two buckets each contain (npartitions + 1) elements,
4145427Sbostic  * with the remaining elements split equally between the first and last
4245427Sbostic  * buckets pushed on the stack.  In this case, stack growth is bounded when:
4345427Sbostic  *
4445346Sbostic  *	for (partition_cnt = 0; nelements > npartitions; ++partition_cnt)
4545345Sbostic  *		nelements =
4645345Sbostic  *		    (nelements - (npartitions + 1) * (nbuckets - 2)) / 2;
4745345Sbostic  * The bound is:
4845345Sbostic  *
4945345Sbostic  *	limit = partition_cnt * (nbuckets - 1);
5045346Sbostic  *
5145427Sbostic  * This is a much smaller number, 4590 for the maximum 32-bit signed int.
5245287Sbostic  */
5345427Sbostic #define	NBUCKETS	(UCHAR_MAX + 1)
5445427Sbostic 
5545287Sbostic typedef struct _stack {
5645287Sbostic 	u_char **bot;
5745287Sbostic 	int indx, nmemb;
5845287Sbostic } CONTEXT;
5945287Sbostic 
6045287Sbostic #define	STACKPUSH { \
6145287Sbostic 	stackp->bot = p; \
6245287Sbostic 	stackp->nmemb = nmemb; \
6345287Sbostic 	stackp->indx = indx; \
6445287Sbostic 	++stackp; \
6545287Sbostic }
6645287Sbostic #define	STACKPOP { \
6745287Sbostic 	if (stackp == stack) \
6845287Sbostic 		break; \
6945287Sbostic 	--stackp; \
7045287Sbostic 	bot = stackp->bot; \
7145287Sbostic 	nmemb = stackp->nmemb; \
7245287Sbostic 	indx = stackp->indx; \
7345287Sbostic }
7445287Sbostic 
7545287Sbostic /*
7645287Sbostic  * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5,
7745345Sbostic  * Ex. 10 and 12.  Also, "Three Partition Refinement Algorithms, Paige
7845345Sbostic  * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987.
7945287Sbostic  *
8045345Sbostic  * This uses a simple sort as soon as a bucket crosses a cutoff point,
8145345Sbostic  * rather than sorting the entire list after partitioning is finished.
8245345Sbostic  * This should be an advantage.
8345287Sbostic  *
8445345Sbostic  * This is pure MSD instead of LSD of some number of MSD, switching to
8545345Sbostic  * the simple sort as soon as possible.  Takes linear time relative to
8645345Sbostic  * the number of bytes in the strings.
8745287Sbostic  */
8845287Sbostic radixsort(l1, nmemb, tab, endbyte)
8945287Sbostic 	u_char **l1, *tab, endbyte;
9045287Sbostic 	register int nmemb;
9145287Sbostic {
9245287Sbostic 	register int i, indx, t1, t2;
9345287Sbostic 	register u_char **l2, **p, **bot, *tr;
9445345Sbostic 	CONTEXT *stack, *stackp;
9545427Sbostic 	int c[NBUCKETS + 1], max;
9645427Sbostic 	u_char ltab[NBUCKETS];
97*45935Sbostic 	static void shellsort();
9845287Sbostic 
9945287Sbostic 	if (nmemb <= 1)
10045287Sbostic 		return(0);
10145287Sbostic 
10245346Sbostic 	/*
10345345Sbostic 	 * T1 is the constant part of the equation, the number of elements
10445345Sbostic 	 * represented on the stack between the top and bottom entries.
10545346Sbostic 	 * It doesn't get rounded as the divide by 2 rounds down (correct
10645346Sbostic 	 * for a value being subtracted).  T2, the nelem value, has to be
10745346Sbostic 	 * rounded up before each divide because we want an upper bound;
10845346Sbostic 	 * this could overflow if nmemb is the maximum int.
10945346Sbostic 	 */
11045427Sbostic 	t1 = ((__rspartition + 1) * (NBUCKETS - 2)) >> 1;
11145427Sbostic 	for (i = 0, t2 = nmemb; t2 > __rspartition; i += NBUCKETS - 1)
11245779Sbostic 		t2 = ((t2 + 1) >> 1) - t1;
11345345Sbostic 	if (i) {
11445346Sbostic 		if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT))))
11545345Sbostic 			return(-1);
11645345Sbostic 	} else
11745345Sbostic 		stack = stackp = NULL;
11845345Sbostic 
11945287Sbostic 	/*
12045345Sbostic 	 * There are two arrays, one provided by the user (l1), and the
12145287Sbostic 	 * temporary one (l2).  The data is sorted to the temporary stack,
12245287Sbostic 	 * and then copied back.  The speedup of using index to determine
12345287Sbostic 	 * which stack the data is on and simply swapping stacks back and
12445287Sbostic 	 * forth, thus avoiding the copy every iteration, turns out to not
12545287Sbostic 	 * be any faster than the current implementation.
12645287Sbostic 	 */
12745287Sbostic 	if (!(l2 = (u_char **)malloc(sizeof(u_char *) * nmemb)))
12845287Sbostic 		return(-1);
12945287Sbostic 
13045287Sbostic 	/*
13145345Sbostic 	 * Tr references a table of sort weights; multiple entries may
13245287Sbostic 	 * map to the same weight; EOS char must have the lowest weight.
13345287Sbostic 	 */
13445287Sbostic 	if (tab)
13545287Sbostic 		tr = tab;
13645287Sbostic 	else {
13745287Sbostic 		tr = ltab;
13845287Sbostic 		for (t1 = 0, t2 = endbyte; t1 < t2; ++t1)
13945287Sbostic 			tr[t1] = t1 + 1;
14045287Sbostic 		tr[t2] = 0;
14145427Sbostic 		for (t1 = endbyte + 1; t1 < NBUCKETS; ++t1)
14245287Sbostic 			tr[t1] = t1;
14345287Sbostic 	}
14445287Sbostic 
14545345Sbostic 	/* First sort is entire stack */
14645287Sbostic 	bot = l1;
14745287Sbostic 	indx = 0;
14845287Sbostic 
14945287Sbostic 	for (;;) {
15045345Sbostic 		/* Clear bucket count array */
15145287Sbostic 		bzero((char *)c, sizeof(c));
15245287Sbostic 
15345287Sbostic 		/*
15445345Sbostic 		 * Compute number of items that sort to the same bucket
15545287Sbostic 		 * for this index.
15645287Sbostic 		 */
157*45935Sbostic 		for (p = bot, i = nmemb; --i >= 0;)
15845287Sbostic 			++c[tr[(*p++)[indx]]];
15945287Sbostic 
16045287Sbostic 		/*
16145345Sbostic 		 * Sum the number of characters into c, dividing the temp
16245287Sbostic 		 * stack into the right number of buckets for this bucket,
16345287Sbostic 		 * this index.  C contains the cumulative total of keys
16445287Sbostic 		 * before and included in this bucket, and will later be
16545427Sbostic 		 * used as an index to the bucket.  c[NBUCKETS] contains
16645287Sbostic 		 * the total number of elements, for determining how many
16745345Sbostic 		 * elements the last bucket contains.  At the same time
16845427Sbostic 		 * find the largest bucket so it gets pushed first.
16945287Sbostic 		 */
17045427Sbostic 		for (i = max = t1 = 0, t2 = __rspartition; i <= NBUCKETS; ++i) {
17145427Sbostic 			if (c[i] > t2) {
17245427Sbostic 				t2 = c[i];
17345345Sbostic 				max = i;
17445345Sbostic 			}
17545427Sbostic 			t1 = c[i] += t1;
17645345Sbostic 		}
17745287Sbostic 
17845287Sbostic 		/*
17945427Sbostic 		 * Partition the elements into buckets; c decrements through
18045427Sbostic 		 * the bucket, and ends up pointing to the first element of
18145427Sbostic 		 * the bucket.
18245287Sbostic 		 */
183*45935Sbostic 		for (i = nmemb; --i >= 0;) {
18445287Sbostic 			--p;
18545287Sbostic 			l2[--c[tr[(*p)[indx]]]] = *p;
18645287Sbostic 		}
18745287Sbostic 
18845345Sbostic 		/* Copy the partitioned elements back to user stack */
18945287Sbostic 		bcopy(l2, bot, nmemb * sizeof(u_char *));
19045287Sbostic 
19145287Sbostic 		++indx;
19245287Sbostic 		/*
19345345Sbostic 		 * Sort buckets as necessary; don't sort c[0], it's the
19445287Sbostic 		 * EOS character bucket, and nothing can follow EOS.
19545287Sbostic 		 */
19645345Sbostic 		for (i = max; i; --i) {
19745287Sbostic 			if ((nmemb = c[i + 1] - (t1 = c[i])) < 2)
19845287Sbostic 				continue;
19945287Sbostic 			p = bot + t1;
20045287Sbostic 			if (nmemb > __rspartition)
20145287Sbostic 				STACKPUSH
20245287Sbostic 			else
203*45935Sbostic 				shellsort(p, indx, nmemb, tr);
20445287Sbostic 		}
20545427Sbostic 		for (i = max + 1; i < NBUCKETS; ++i) {
20645345Sbostic 			if ((nmemb = c[i + 1] - (t1 = c[i])) < 2)
20745345Sbostic 				continue;
20845345Sbostic 			p = bot + t1;
20945345Sbostic 			if (nmemb > __rspartition)
21045345Sbostic 				STACKPUSH
21145345Sbostic 			else
212*45935Sbostic 				shellsort(p, indx, nmemb, tr);
21345345Sbostic 		}
21445345Sbostic 		/* Break out when stack is empty */
21545287Sbostic 		STACKPOP
21645287Sbostic 	}
21745287Sbostic 
21845287Sbostic 	free((char *)l2);
21945287Sbostic 	free((char *)stack);
22045287Sbostic 	return(0);
22145287Sbostic }
222*45935Sbostic 
223*45935Sbostic /*
224*45935Sbostic  * Shellsort (diminishing increment sort) from Data Structures and
225*45935Sbostic  * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290;
226*45935Sbostic  * see also Knuth Vol. 3, page 84.  The increments are selected from
227*45935Sbostic  * formula (8), page 95.  Roughly O(N^3/2).
228*45935Sbostic  */
229*45935Sbostic static void
230*45935Sbostic shellsort(p, indx, nmemb, tr)
231*45935Sbostic 	register u_char **p, *tr;
232*45935Sbostic 	register int indx, nmemb;
233*45935Sbostic {
234*45935Sbostic 	register u_char ch, *s1, *s2;
235*45935Sbostic 	register int incr, *incrp, t1, t2;
236*45935Sbostic 
237*45935Sbostic 	for (incrp = __rsshell_increments; incr = *incrp++;)
238*45935Sbostic 		for (t1 = incr; t1 < nmemb; ++t1)
239*45935Sbostic 			for (t2 = t1 - incr; t2 >= 0;) {
240*45935Sbostic 				s1 = p[t2] + indx;
241*45935Sbostic 				s2 = p[t2 + incr] + indx;
242*45935Sbostic 				while ((ch = tr[*s1++]) == tr[*s2] && ch)
243*45935Sbostic 					++s2;
244*45935Sbostic 				if (ch > tr[*s2]) {
245*45935Sbostic 					s1 = p[t2];
246*45935Sbostic 					p[t2] = p[t2 + incr];
247*45935Sbostic 					p[t2 + incr] = s1;
248*45935Sbostic 					t2 -= incr;
249*45935Sbostic 				} else
250*45935Sbostic 					break;
251*45935Sbostic 			}
252*45935Sbostic }
253