145287Sbostic /*- 245287Sbostic * Copyright (c) 1990 The Regents of the University of California. 345287Sbostic * All rights reserved. 445287Sbostic * 5*50859Sbostic * This code is derived from software contributed to Berkeley by 6*50859Sbostic * Dan Bernstein at New York University. 7*50859Sbostic * 845287Sbostic * %sccs.include.redist.c% 945287Sbostic */ 1045287Sbostic 1145287Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*50859Sbostic static char sccsid[] = "@(#)radixsort.c 5.9 (Berkeley) 08/17/91"; 1345287Sbostic #endif /* LIBC_SCCS and not lint */ 1445287Sbostic 1545287Sbostic #include <sys/types.h> 1645287Sbostic #include <limits.h> 1745287Sbostic #include <stdlib.h> 1845287Sbostic #include <stddef.h> 1946599Sdonn #include <string.h> 2045287Sbostic 2145287Sbostic /* 2245287Sbostic * __rspartition is the cutoff point for a further partitioning instead 2345287Sbostic * of a shellsort. If it changes check __rsshell_increments. Both of 2445935Sbostic * these are exported, as the best values are data dependent. 2545287Sbostic */ 2645287Sbostic #define NPARTITION 40 2745287Sbostic int __rspartition = NPARTITION; 2845287Sbostic int __rsshell_increments[] = { 4, 1, 0, 0, 0, 0, 0, 0 }; 2945287Sbostic 3045287Sbostic /* 3145345Sbostic * Stackp points to context structures, where each structure schedules a 3245345Sbostic * partitioning. Radixsort exits when the stack is empty. 3345287Sbostic * 3445346Sbostic * If the buckets are placed on the stack randomly, the worst case is when 3545427Sbostic * all the buckets but one contain (npartitions + 1) elements and the bucket 3645346Sbostic * pushed on the stack last contains the rest of the elements. In this case, 3745346Sbostic * stack growth is bounded by: 3845345Sbostic * 3945427Sbostic * limit = (nelements / (npartitions + 1)) - 1; 4045345Sbostic * 4145427Sbostic * This is a very large number, 52,377,648 for the maximum 32-bit signed int. 4245345Sbostic * 4345427Sbostic * By forcing the largest bucket to be pushed on the stack first, the worst 4445427Sbostic * case is when all but two buckets each contain (npartitions + 1) elements, 4545427Sbostic * with the remaining elements split equally between the first and last 4645427Sbostic * buckets pushed on the stack. In this case, stack growth is bounded when: 4745427Sbostic * 4845346Sbostic * for (partition_cnt = 0; nelements > npartitions; ++partition_cnt) 4945345Sbostic * nelements = 5045345Sbostic * (nelements - (npartitions + 1) * (nbuckets - 2)) / 2; 5145345Sbostic * The bound is: 5245345Sbostic * 5345345Sbostic * limit = partition_cnt * (nbuckets - 1); 5445346Sbostic * 5545427Sbostic * This is a much smaller number, 4590 for the maximum 32-bit signed int. 5645287Sbostic */ 5745427Sbostic #define NBUCKETS (UCHAR_MAX + 1) 5845427Sbostic 5945287Sbostic typedef struct _stack { 6046599Sdonn const u_char **bot; 6145287Sbostic int indx, nmemb; 6245287Sbostic } CONTEXT; 6345287Sbostic 6445287Sbostic #define STACKPUSH { \ 6545287Sbostic stackp->bot = p; \ 6645287Sbostic stackp->nmemb = nmemb; \ 6745287Sbostic stackp->indx = indx; \ 6845287Sbostic ++stackp; \ 6945287Sbostic } 7045287Sbostic #define STACKPOP { \ 7145287Sbostic if (stackp == stack) \ 7245287Sbostic break; \ 7345287Sbostic --stackp; \ 7445287Sbostic bot = stackp->bot; \ 7545287Sbostic nmemb = stackp->nmemb; \ 7645287Sbostic indx = stackp->indx; \ 7745287Sbostic } 7845287Sbostic 7945287Sbostic /* 8045287Sbostic * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5, 8145345Sbostic * Ex. 10 and 12. Also, "Three Partition Refinement Algorithms, Paige 8245345Sbostic * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987. 8345287Sbostic * 8445345Sbostic * This uses a simple sort as soon as a bucket crosses a cutoff point, 8545345Sbostic * rather than sorting the entire list after partitioning is finished. 8645345Sbostic * This should be an advantage. 8745287Sbostic * 8845345Sbostic * This is pure MSD instead of LSD of some number of MSD, switching to 8945345Sbostic * the simple sort as soon as possible. Takes linear time relative to 9045345Sbostic * the number of bytes in the strings. 9145287Sbostic */ 9246599Sdonn int 9345287Sbostic radixsort(l1, nmemb, tab, endbyte) 9446599Sdonn const u_char **l1; 9545287Sbostic register int nmemb; 9646599Sdonn const u_char *tab; 9750481Sbostic u_int endbyte; 9845287Sbostic { 9945287Sbostic register int i, indx, t1, t2; 10046599Sdonn register const u_char **l2; 10146599Sdonn register const u_char **p; 10246599Sdonn register const u_char **bot; 10346599Sdonn register const u_char *tr; 10445345Sbostic CONTEXT *stack, *stackp; 10545427Sbostic int c[NBUCKETS + 1], max; 10645427Sbostic u_char ltab[NBUCKETS]; 10745935Sbostic static void shellsort(); 10845287Sbostic 10945287Sbostic if (nmemb <= 1) 11045287Sbostic return(0); 11145287Sbostic 11245346Sbostic /* 11345345Sbostic * T1 is the constant part of the equation, the number of elements 11445345Sbostic * represented on the stack between the top and bottom entries. 11545346Sbostic * It doesn't get rounded as the divide by 2 rounds down (correct 11645346Sbostic * for a value being subtracted). T2, the nelem value, has to be 11745346Sbostic * rounded up before each divide because we want an upper bound; 11845346Sbostic * this could overflow if nmemb is the maximum int. 11945346Sbostic */ 12045427Sbostic t1 = ((__rspartition + 1) * (NBUCKETS - 2)) >> 1; 12145427Sbostic for (i = 0, t2 = nmemb; t2 > __rspartition; i += NBUCKETS - 1) 12245779Sbostic t2 = ((t2 + 1) >> 1) - t1; 12345345Sbostic if (i) { 12445346Sbostic if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 12545345Sbostic return(-1); 12645345Sbostic } else 12745345Sbostic stack = stackp = NULL; 12845345Sbostic 12945287Sbostic /* 13045345Sbostic * There are two arrays, one provided by the user (l1), and the 13145287Sbostic * temporary one (l2). The data is sorted to the temporary stack, 13245287Sbostic * and then copied back. The speedup of using index to determine 13345287Sbostic * which stack the data is on and simply swapping stacks back and 13445287Sbostic * forth, thus avoiding the copy every iteration, turns out to not 13545287Sbostic * be any faster than the current implementation. 13645287Sbostic */ 13746599Sdonn if (!(l2 = (const u_char **)malloc(sizeof(u_char *) * nmemb))) 13845287Sbostic return(-1); 13945287Sbostic 14045287Sbostic /* 14145345Sbostic * Tr references a table of sort weights; multiple entries may 14245287Sbostic * map to the same weight; EOS char must have the lowest weight. 14345287Sbostic */ 14445287Sbostic if (tab) 14545287Sbostic tr = tab; 14645287Sbostic else { 14745287Sbostic for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 14846599Sdonn ltab[t1] = t1 + 1; 14946599Sdonn ltab[t2] = 0; 15045427Sbostic for (t1 = endbyte + 1; t1 < NBUCKETS; ++t1) 15146599Sdonn ltab[t1] = t1; 15246599Sdonn tr = ltab; 15345287Sbostic } 15445287Sbostic 15545345Sbostic /* First sort is entire stack */ 15645287Sbostic bot = l1; 15745287Sbostic indx = 0; 15845287Sbostic 15945287Sbostic for (;;) { 16045345Sbostic /* Clear bucket count array */ 16145287Sbostic bzero((char *)c, sizeof(c)); 16245287Sbostic 16345287Sbostic /* 16445345Sbostic * Compute number of items that sort to the same bucket 16545287Sbostic * for this index. 16645287Sbostic */ 16745935Sbostic for (p = bot, i = nmemb; --i >= 0;) 16845287Sbostic ++c[tr[(*p++)[indx]]]; 16945287Sbostic 17045287Sbostic /* 17145345Sbostic * Sum the number of characters into c, dividing the temp 17245287Sbostic * stack into the right number of buckets for this bucket, 17345287Sbostic * this index. C contains the cumulative total of keys 17445287Sbostic * before and included in this bucket, and will later be 17545427Sbostic * used as an index to the bucket. c[NBUCKETS] contains 17645287Sbostic * the total number of elements, for determining how many 17745345Sbostic * elements the last bucket contains. At the same time 17845427Sbostic * find the largest bucket so it gets pushed first. 17945287Sbostic */ 18045427Sbostic for (i = max = t1 = 0, t2 = __rspartition; i <= NBUCKETS; ++i) { 18145427Sbostic if (c[i] > t2) { 18245427Sbostic t2 = c[i]; 18345345Sbostic max = i; 18445345Sbostic } 18545427Sbostic t1 = c[i] += t1; 18645345Sbostic } 18745287Sbostic 18845287Sbostic /* 18945427Sbostic * Partition the elements into buckets; c decrements through 19045427Sbostic * the bucket, and ends up pointing to the first element of 19145427Sbostic * the bucket. 19245287Sbostic */ 19345935Sbostic for (i = nmemb; --i >= 0;) { 19445287Sbostic --p; 19545287Sbostic l2[--c[tr[(*p)[indx]]]] = *p; 19645287Sbostic } 19745287Sbostic 19845345Sbostic /* Copy the partitioned elements back to user stack */ 19945287Sbostic bcopy(l2, bot, nmemb * sizeof(u_char *)); 20045287Sbostic 20145287Sbostic ++indx; 20245287Sbostic /* 20345345Sbostic * Sort buckets as necessary; don't sort c[0], it's the 20445287Sbostic * EOS character bucket, and nothing can follow EOS. 20545287Sbostic */ 20645345Sbostic for (i = max; i; --i) { 20745287Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 20845287Sbostic continue; 20945287Sbostic p = bot + t1; 21045287Sbostic if (nmemb > __rspartition) 21145287Sbostic STACKPUSH 21245287Sbostic else 21345935Sbostic shellsort(p, indx, nmemb, tr); 21445287Sbostic } 21545427Sbostic for (i = max + 1; i < NBUCKETS; ++i) { 21645345Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 21745345Sbostic continue; 21845345Sbostic p = bot + t1; 21945345Sbostic if (nmemb > __rspartition) 22045345Sbostic STACKPUSH 22145345Sbostic else 22245935Sbostic shellsort(p, indx, nmemb, tr); 22345345Sbostic } 22445345Sbostic /* Break out when stack is empty */ 22545287Sbostic STACKPOP 22645287Sbostic } 22745287Sbostic 22845287Sbostic free((char *)l2); 22945287Sbostic free((char *)stack); 23045287Sbostic return(0); 23145287Sbostic } 23245935Sbostic 23345935Sbostic /* 23445935Sbostic * Shellsort (diminishing increment sort) from Data Structures and 23545935Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 23645935Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 23745935Sbostic * formula (8), page 95. Roughly O(N^3/2). 23845935Sbostic */ 23945935Sbostic static void 24045935Sbostic shellsort(p, indx, nmemb, tr) 24145935Sbostic register u_char **p, *tr; 24245935Sbostic register int indx, nmemb; 24345935Sbostic { 24445935Sbostic register u_char ch, *s1, *s2; 24545935Sbostic register int incr, *incrp, t1, t2; 24645935Sbostic 24745935Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 24845935Sbostic for (t1 = incr; t1 < nmemb; ++t1) 24945935Sbostic for (t2 = t1 - incr; t2 >= 0;) { 25045935Sbostic s1 = p[t2] + indx; 25145935Sbostic s2 = p[t2 + incr] + indx; 25245935Sbostic while ((ch = tr[*s1++]) == tr[*s2] && ch) 25345935Sbostic ++s2; 25445935Sbostic if (ch > tr[*s2]) { 25545935Sbostic s1 = p[t2]; 25645935Sbostic p[t2] = p[t2 + incr]; 25745935Sbostic p[t2 + incr] = s1; 25845935Sbostic t2 -= incr; 25945935Sbostic } else 26045935Sbostic break; 26145935Sbostic } 26245935Sbostic } 263