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*50481Sbostic static char sccsid[] = "@(#)radixsort.c 5.8 (Berkeley) 07/23/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> 1646599Sdonn #include <string.h> 1745287Sbostic 1845287Sbostic /* 1945287Sbostic * __rspartition is the cutoff point for a further partitioning instead 2045287Sbostic * of a shellsort. If it changes check __rsshell_increments. Both of 2145935Sbostic * these are exported, as the best values are data dependent. 2245287Sbostic */ 2345287Sbostic #define NPARTITION 40 2445287Sbostic int __rspartition = NPARTITION; 2545287Sbostic int __rsshell_increments[] = { 4, 1, 0, 0, 0, 0, 0, 0 }; 2645287Sbostic 2745287Sbostic /* 2845345Sbostic * Stackp points to context structures, where each structure schedules a 2945345Sbostic * partitioning. Radixsort exits when the stack is empty. 3045287Sbostic * 3145346Sbostic * If the buckets are placed on the stack randomly, the worst case is when 3245427Sbostic * all the buckets but one contain (npartitions + 1) elements and the bucket 3345346Sbostic * pushed on the stack last contains the rest of the elements. In this case, 3445346Sbostic * stack growth is bounded by: 3545345Sbostic * 3645427Sbostic * limit = (nelements / (npartitions + 1)) - 1; 3745345Sbostic * 3845427Sbostic * This is a very large number, 52,377,648 for the maximum 32-bit signed int. 3945345Sbostic * 4045427Sbostic * By forcing the largest bucket to be pushed on the stack first, the worst 4145427Sbostic * case is when all but two buckets each contain (npartitions + 1) elements, 4245427Sbostic * with the remaining elements split equally between the first and last 4345427Sbostic * buckets pushed on the stack. In this case, stack growth is bounded when: 4445427Sbostic * 4545346Sbostic * for (partition_cnt = 0; nelements > npartitions; ++partition_cnt) 4645345Sbostic * nelements = 4745345Sbostic * (nelements - (npartitions + 1) * (nbuckets - 2)) / 2; 4845345Sbostic * The bound is: 4945345Sbostic * 5045345Sbostic * limit = partition_cnt * (nbuckets - 1); 5145346Sbostic * 5245427Sbostic * This is a much smaller number, 4590 for the maximum 32-bit signed int. 5345287Sbostic */ 5445427Sbostic #define NBUCKETS (UCHAR_MAX + 1) 5545427Sbostic 5645287Sbostic typedef struct _stack { 5746599Sdonn const u_char **bot; 5845287Sbostic int indx, nmemb; 5945287Sbostic } CONTEXT; 6045287Sbostic 6145287Sbostic #define STACKPUSH { \ 6245287Sbostic stackp->bot = p; \ 6345287Sbostic stackp->nmemb = nmemb; \ 6445287Sbostic stackp->indx = indx; \ 6545287Sbostic ++stackp; \ 6645287Sbostic } 6745287Sbostic #define STACKPOP { \ 6845287Sbostic if (stackp == stack) \ 6945287Sbostic break; \ 7045287Sbostic --stackp; \ 7145287Sbostic bot = stackp->bot; \ 7245287Sbostic nmemb = stackp->nmemb; \ 7345287Sbostic indx = stackp->indx; \ 7445287Sbostic } 7545287Sbostic 7645287Sbostic /* 7745287Sbostic * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5, 7845345Sbostic * Ex. 10 and 12. Also, "Three Partition Refinement Algorithms, Paige 7945345Sbostic * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987. 8045287Sbostic * 8145345Sbostic * This uses a simple sort as soon as a bucket crosses a cutoff point, 8245345Sbostic * rather than sorting the entire list after partitioning is finished. 8345345Sbostic * This should be an advantage. 8445287Sbostic * 8545345Sbostic * This is pure MSD instead of LSD of some number of MSD, switching to 8645345Sbostic * the simple sort as soon as possible. Takes linear time relative to 8745345Sbostic * the number of bytes in the strings. 8845287Sbostic */ 8946599Sdonn int 9045287Sbostic radixsort(l1, nmemb, tab, endbyte) 9146599Sdonn const u_char **l1; 9245287Sbostic register int nmemb; 9346599Sdonn const u_char *tab; 94*50481Sbostic u_int endbyte; 9545287Sbostic { 9645287Sbostic register int i, indx, t1, t2; 9746599Sdonn register const u_char **l2; 9846599Sdonn register const u_char **p; 9946599Sdonn register const u_char **bot; 10046599Sdonn register const u_char *tr; 10145345Sbostic CONTEXT *stack, *stackp; 10245427Sbostic int c[NBUCKETS + 1], max; 10345427Sbostic u_char ltab[NBUCKETS]; 10445935Sbostic static void shellsort(); 10545287Sbostic 10645287Sbostic if (nmemb <= 1) 10745287Sbostic return(0); 10845287Sbostic 10945346Sbostic /* 11045345Sbostic * T1 is the constant part of the equation, the number of elements 11145345Sbostic * represented on the stack between the top and bottom entries. 11245346Sbostic * It doesn't get rounded as the divide by 2 rounds down (correct 11345346Sbostic * for a value being subtracted). T2, the nelem value, has to be 11445346Sbostic * rounded up before each divide because we want an upper bound; 11545346Sbostic * this could overflow if nmemb is the maximum int. 11645346Sbostic */ 11745427Sbostic t1 = ((__rspartition + 1) * (NBUCKETS - 2)) >> 1; 11845427Sbostic for (i = 0, t2 = nmemb; t2 > __rspartition; i += NBUCKETS - 1) 11945779Sbostic t2 = ((t2 + 1) >> 1) - t1; 12045345Sbostic if (i) { 12145346Sbostic if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 12245345Sbostic return(-1); 12345345Sbostic } else 12445345Sbostic stack = stackp = NULL; 12545345Sbostic 12645287Sbostic /* 12745345Sbostic * There are two arrays, one provided by the user (l1), and the 12845287Sbostic * temporary one (l2). The data is sorted to the temporary stack, 12945287Sbostic * and then copied back. The speedup of using index to determine 13045287Sbostic * which stack the data is on and simply swapping stacks back and 13145287Sbostic * forth, thus avoiding the copy every iteration, turns out to not 13245287Sbostic * be any faster than the current implementation. 13345287Sbostic */ 13446599Sdonn if (!(l2 = (const u_char **)malloc(sizeof(u_char *) * nmemb))) 13545287Sbostic return(-1); 13645287Sbostic 13745287Sbostic /* 13845345Sbostic * Tr references a table of sort weights; multiple entries may 13945287Sbostic * map to the same weight; EOS char must have the lowest weight. 14045287Sbostic */ 14145287Sbostic if (tab) 14245287Sbostic tr = tab; 14345287Sbostic else { 14445287Sbostic for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 14546599Sdonn ltab[t1] = t1 + 1; 14646599Sdonn ltab[t2] = 0; 14745427Sbostic for (t1 = endbyte + 1; t1 < NBUCKETS; ++t1) 14846599Sdonn ltab[t1] = t1; 14946599Sdonn tr = ltab; 15045287Sbostic } 15145287Sbostic 15245345Sbostic /* First sort is entire stack */ 15345287Sbostic bot = l1; 15445287Sbostic indx = 0; 15545287Sbostic 15645287Sbostic for (;;) { 15745345Sbostic /* Clear bucket count array */ 15845287Sbostic bzero((char *)c, sizeof(c)); 15945287Sbostic 16045287Sbostic /* 16145345Sbostic * Compute number of items that sort to the same bucket 16245287Sbostic * for this index. 16345287Sbostic */ 16445935Sbostic for (p = bot, i = nmemb; --i >= 0;) 16545287Sbostic ++c[tr[(*p++)[indx]]]; 16645287Sbostic 16745287Sbostic /* 16845345Sbostic * Sum the number of characters into c, dividing the temp 16945287Sbostic * stack into the right number of buckets for this bucket, 17045287Sbostic * this index. C contains the cumulative total of keys 17145287Sbostic * before and included in this bucket, and will later be 17245427Sbostic * used as an index to the bucket. c[NBUCKETS] contains 17345287Sbostic * the total number of elements, for determining how many 17445345Sbostic * elements the last bucket contains. At the same time 17545427Sbostic * find the largest bucket so it gets pushed first. 17645287Sbostic */ 17745427Sbostic for (i = max = t1 = 0, t2 = __rspartition; i <= NBUCKETS; ++i) { 17845427Sbostic if (c[i] > t2) { 17945427Sbostic t2 = c[i]; 18045345Sbostic max = i; 18145345Sbostic } 18245427Sbostic t1 = c[i] += t1; 18345345Sbostic } 18445287Sbostic 18545287Sbostic /* 18645427Sbostic * Partition the elements into buckets; c decrements through 18745427Sbostic * the bucket, and ends up pointing to the first element of 18845427Sbostic * the bucket. 18945287Sbostic */ 19045935Sbostic for (i = nmemb; --i >= 0;) { 19145287Sbostic --p; 19245287Sbostic l2[--c[tr[(*p)[indx]]]] = *p; 19345287Sbostic } 19445287Sbostic 19545345Sbostic /* Copy the partitioned elements back to user stack */ 19645287Sbostic bcopy(l2, bot, nmemb * sizeof(u_char *)); 19745287Sbostic 19845287Sbostic ++indx; 19945287Sbostic /* 20045345Sbostic * Sort buckets as necessary; don't sort c[0], it's the 20145287Sbostic * EOS character bucket, and nothing can follow EOS. 20245287Sbostic */ 20345345Sbostic for (i = max; i; --i) { 20445287Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 20545287Sbostic continue; 20645287Sbostic p = bot + t1; 20745287Sbostic if (nmemb > __rspartition) 20845287Sbostic STACKPUSH 20945287Sbostic else 21045935Sbostic shellsort(p, indx, nmemb, tr); 21145287Sbostic } 21245427Sbostic for (i = max + 1; i < NBUCKETS; ++i) { 21345345Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 21445345Sbostic continue; 21545345Sbostic p = bot + t1; 21645345Sbostic if (nmemb > __rspartition) 21745345Sbostic STACKPUSH 21845345Sbostic else 21945935Sbostic shellsort(p, indx, nmemb, tr); 22045345Sbostic } 22145345Sbostic /* Break out when stack is empty */ 22245287Sbostic STACKPOP 22345287Sbostic } 22445287Sbostic 22545287Sbostic free((char *)l2); 22645287Sbostic free((char *)stack); 22745287Sbostic return(0); 22845287Sbostic } 22945935Sbostic 23045935Sbostic /* 23145935Sbostic * Shellsort (diminishing increment sort) from Data Structures and 23245935Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 23345935Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 23445935Sbostic * formula (8), page 95. Roughly O(N^3/2). 23545935Sbostic */ 23645935Sbostic static void 23745935Sbostic shellsort(p, indx, nmemb, tr) 23845935Sbostic register u_char **p, *tr; 23945935Sbostic register int indx, nmemb; 24045935Sbostic { 24145935Sbostic register u_char ch, *s1, *s2; 24245935Sbostic register int incr, *incrp, t1, t2; 24345935Sbostic 24445935Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 24545935Sbostic for (t1 = incr; t1 < nmemb; ++t1) 24645935Sbostic for (t2 = t1 - incr; t2 >= 0;) { 24745935Sbostic s1 = p[t2] + indx; 24845935Sbostic s2 = p[t2 + incr] + indx; 24945935Sbostic while ((ch = tr[*s1++]) == tr[*s2] && ch) 25045935Sbostic ++s2; 25145935Sbostic if (ch > tr[*s2]) { 25245935Sbostic s1 = p[t2]; 25345935Sbostic p[t2] = p[t2 + incr]; 25445935Sbostic p[t2 + incr] = s1; 25545935Sbostic t2 -= incr; 25645935Sbostic } else 25745935Sbostic break; 25845935Sbostic } 25945935Sbostic } 260