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*46599Sdonn static char sccsid[] = "@(#)radixsort.c 5.7 (Berkeley) 02/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> 16*46599Sdonn #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 { 57*46599Sdonn 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 */ 89*46599Sdonn int 90*46599Sdonn #if __STDC__ 91*46599Sdonn radixsort(const u_char **l1, int nmemb, const u_char *tab, u_char endbyte) 92*46599Sdonn #else 9345287Sbostic radixsort(l1, nmemb, tab, endbyte) 94*46599Sdonn const u_char **l1; 9545287Sbostic register int nmemb; 96*46599Sdonn const u_char *tab; 97*46599Sdonn u_char endbyte; 98*46599Sdonn #endif 9945287Sbostic { 10045287Sbostic register int i, indx, t1, t2; 101*46599Sdonn register const u_char **l2; 102*46599Sdonn register const u_char **p; 103*46599Sdonn register const u_char **bot; 104*46599Sdonn register const u_char *tr; 10545345Sbostic CONTEXT *stack, *stackp; 10645427Sbostic int c[NBUCKETS + 1], max; 10745427Sbostic u_char ltab[NBUCKETS]; 10845935Sbostic static void shellsort(); 10945287Sbostic 11045287Sbostic if (nmemb <= 1) 11145287Sbostic return(0); 11245287Sbostic 11345346Sbostic /* 11445345Sbostic * T1 is the constant part of the equation, the number of elements 11545345Sbostic * represented on the stack between the top and bottom entries. 11645346Sbostic * It doesn't get rounded as the divide by 2 rounds down (correct 11745346Sbostic * for a value being subtracted). T2, the nelem value, has to be 11845346Sbostic * rounded up before each divide because we want an upper bound; 11945346Sbostic * this could overflow if nmemb is the maximum int. 12045346Sbostic */ 12145427Sbostic t1 = ((__rspartition + 1) * (NBUCKETS - 2)) >> 1; 12245427Sbostic for (i = 0, t2 = nmemb; t2 > __rspartition; i += NBUCKETS - 1) 12345779Sbostic t2 = ((t2 + 1) >> 1) - t1; 12445345Sbostic if (i) { 12545346Sbostic if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 12645345Sbostic return(-1); 12745345Sbostic } else 12845345Sbostic stack = stackp = NULL; 12945345Sbostic 13045287Sbostic /* 13145345Sbostic * There are two arrays, one provided by the user (l1), and the 13245287Sbostic * temporary one (l2). The data is sorted to the temporary stack, 13345287Sbostic * and then copied back. The speedup of using index to determine 13445287Sbostic * which stack the data is on and simply swapping stacks back and 13545287Sbostic * forth, thus avoiding the copy every iteration, turns out to not 13645287Sbostic * be any faster than the current implementation. 13745287Sbostic */ 138*46599Sdonn if (!(l2 = (const u_char **)malloc(sizeof(u_char *) * nmemb))) 13945287Sbostic return(-1); 14045287Sbostic 14145287Sbostic /* 14245345Sbostic * Tr references a table of sort weights; multiple entries may 14345287Sbostic * map to the same weight; EOS char must have the lowest weight. 14445287Sbostic */ 14545287Sbostic if (tab) 14645287Sbostic tr = tab; 14745287Sbostic else { 14845287Sbostic for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 149*46599Sdonn ltab[t1] = t1 + 1; 150*46599Sdonn ltab[t2] = 0; 15145427Sbostic for (t1 = endbyte + 1; t1 < NBUCKETS; ++t1) 152*46599Sdonn ltab[t1] = t1; 153*46599Sdonn tr = ltab; 15445287Sbostic } 15545287Sbostic 15645345Sbostic /* First sort is entire stack */ 15745287Sbostic bot = l1; 15845287Sbostic indx = 0; 15945287Sbostic 16045287Sbostic for (;;) { 16145345Sbostic /* Clear bucket count array */ 16245287Sbostic bzero((char *)c, sizeof(c)); 16345287Sbostic 16445287Sbostic /* 16545345Sbostic * Compute number of items that sort to the same bucket 16645287Sbostic * for this index. 16745287Sbostic */ 16845935Sbostic for (p = bot, i = nmemb; --i >= 0;) 16945287Sbostic ++c[tr[(*p++)[indx]]]; 17045287Sbostic 17145287Sbostic /* 17245345Sbostic * Sum the number of characters into c, dividing the temp 17345287Sbostic * stack into the right number of buckets for this bucket, 17445287Sbostic * this index. C contains the cumulative total of keys 17545287Sbostic * before and included in this bucket, and will later be 17645427Sbostic * used as an index to the bucket. c[NBUCKETS] contains 17745287Sbostic * the total number of elements, for determining how many 17845345Sbostic * elements the last bucket contains. At the same time 17945427Sbostic * find the largest bucket so it gets pushed first. 18045287Sbostic */ 18145427Sbostic for (i = max = t1 = 0, t2 = __rspartition; i <= NBUCKETS; ++i) { 18245427Sbostic if (c[i] > t2) { 18345427Sbostic t2 = c[i]; 18445345Sbostic max = i; 18545345Sbostic } 18645427Sbostic t1 = c[i] += t1; 18745345Sbostic } 18845287Sbostic 18945287Sbostic /* 19045427Sbostic * Partition the elements into buckets; c decrements through 19145427Sbostic * the bucket, and ends up pointing to the first element of 19245427Sbostic * the bucket. 19345287Sbostic */ 19445935Sbostic for (i = nmemb; --i >= 0;) { 19545287Sbostic --p; 19645287Sbostic l2[--c[tr[(*p)[indx]]]] = *p; 19745287Sbostic } 19845287Sbostic 19945345Sbostic /* Copy the partitioned elements back to user stack */ 20045287Sbostic bcopy(l2, bot, nmemb * sizeof(u_char *)); 20145287Sbostic 20245287Sbostic ++indx; 20345287Sbostic /* 20445345Sbostic * Sort buckets as necessary; don't sort c[0], it's the 20545287Sbostic * EOS character bucket, and nothing can follow EOS. 20645287Sbostic */ 20745345Sbostic for (i = max; i; --i) { 20845287Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 20945287Sbostic continue; 21045287Sbostic p = bot + t1; 21145287Sbostic if (nmemb > __rspartition) 21245287Sbostic STACKPUSH 21345287Sbostic else 21445935Sbostic shellsort(p, indx, nmemb, tr); 21545287Sbostic } 21645427Sbostic for (i = max + 1; i < NBUCKETS; ++i) { 21745345Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 21845345Sbostic continue; 21945345Sbostic p = bot + t1; 22045345Sbostic if (nmemb > __rspartition) 22145345Sbostic STACKPUSH 22245345Sbostic else 22345935Sbostic shellsort(p, indx, nmemb, tr); 22445345Sbostic } 22545345Sbostic /* Break out when stack is empty */ 22645287Sbostic STACKPOP 22745287Sbostic } 22845287Sbostic 22945287Sbostic free((char *)l2); 23045287Sbostic free((char *)stack); 23145287Sbostic return(0); 23245287Sbostic } 23345935Sbostic 23445935Sbostic /* 23545935Sbostic * Shellsort (diminishing increment sort) from Data Structures and 23645935Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 23745935Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 23845935Sbostic * formula (8), page 95. Roughly O(N^3/2). 23945935Sbostic */ 24045935Sbostic static void 24145935Sbostic shellsort(p, indx, nmemb, tr) 24245935Sbostic register u_char **p, *tr; 24345935Sbostic register int indx, nmemb; 24445935Sbostic { 24545935Sbostic register u_char ch, *s1, *s2; 24645935Sbostic register int incr, *incrp, t1, t2; 24745935Sbostic 24845935Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) 24945935Sbostic for (t1 = incr; t1 < nmemb; ++t1) 25045935Sbostic for (t2 = t1 - incr; t2 >= 0;) { 25145935Sbostic s1 = p[t2] + indx; 25245935Sbostic s2 = p[t2 + incr] + indx; 25345935Sbostic while ((ch = tr[*s1++]) == tr[*s2] && ch) 25445935Sbostic ++s2; 25545935Sbostic if (ch > tr[*s2]) { 25645935Sbostic s1 = p[t2]; 25745935Sbostic p[t2] = p[t2 + incr]; 25845935Sbostic p[t2 + incr] = s1; 25945935Sbostic t2 -= incr; 26045935Sbostic } else 26145935Sbostic break; 26245935Sbostic } 26345935Sbostic } 264