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*45779Sbostic static char sccsid[] = "@(#)radixsort.c 5.5 (Berkeley) 12/14/90"; 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 /* 1845345Sbostic * Shellsort (diminishing increment sort) from Data Structures and 1945287Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 2045287Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 2145287Sbostic * formula (8), page 95. Roughly O(N^3/2). 2245287Sbostic * 2345287Sbostic * __rspartition is the cutoff point for a further partitioning instead 2445287Sbostic * of a shellsort. If it changes check __rsshell_increments. Both of 2545287Sbostic * these are exported, as the best values are data dependent. Unrolling 2645287Sbostic * this loop has not proven worthwhile. 2745287Sbostic */ 2845287Sbostic #define NPARTITION 40 2945287Sbostic int __rspartition = NPARTITION; 3045287Sbostic int __rsshell_increments[] = { 4, 1, 0, 0, 0, 0, 0, 0 }; 3145287Sbostic #define SHELLSORT { \ 3245287Sbostic register u_char ch, *s1, *s2; \ 3345287Sbostic register int incr, *incrp; \ 3445287Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) \ 3545287Sbostic for (t1 = incr; t1 < nmemb; ++t1) \ 3645287Sbostic for (t2 = t1 - incr; t2 >= 0;) { \ 3745287Sbostic s1 = p[t2] + indx; \ 3845287Sbostic s2 = p[t2 + incr] + indx; \ 3945287Sbostic while ((ch = tr[*s1++]) == tr[*s2] && ch) \ 4045287Sbostic ++s2; \ 4145287Sbostic if (ch > tr[*s2]) { \ 4245287Sbostic s1 = p[t2]; \ 4345287Sbostic p[t2] = p[t2 + incr]; \ 4445287Sbostic p[t2 + incr] = s1; \ 4545287Sbostic t2 -= incr; \ 4645287Sbostic } else \ 4745287Sbostic break; \ 4845287Sbostic } \ 4945287Sbostic } 5045287Sbostic 5145287Sbostic /* 5245345Sbostic * Stackp points to context structures, where each structure schedules a 5345345Sbostic * partitioning. Radixsort exits when the stack is empty. 5445287Sbostic * 5545346Sbostic * If the buckets are placed on the stack randomly, the worst case is when 5645427Sbostic * all the buckets but one contain (npartitions + 1) elements and the bucket 5745346Sbostic * pushed on the stack last contains the rest of the elements. In this case, 5845346Sbostic * stack growth is bounded by: 5945345Sbostic * 6045427Sbostic * limit = (nelements / (npartitions + 1)) - 1; 6145345Sbostic * 6245427Sbostic * This is a very large number, 52,377,648 for the maximum 32-bit signed int. 6345345Sbostic * 6445427Sbostic * By forcing the largest bucket to be pushed on the stack first, the worst 6545427Sbostic * case is when all but two buckets each contain (npartitions + 1) elements, 6645427Sbostic * with the remaining elements split equally between the first and last 6745427Sbostic * buckets pushed on the stack. In this case, stack growth is bounded when: 6845427Sbostic * 6945346Sbostic * for (partition_cnt = 0; nelements > npartitions; ++partition_cnt) 7045345Sbostic * nelements = 7145345Sbostic * (nelements - (npartitions + 1) * (nbuckets - 2)) / 2; 7245345Sbostic * The bound is: 7345345Sbostic * 7445345Sbostic * limit = partition_cnt * (nbuckets - 1); 7545346Sbostic * 7645427Sbostic * This is a much smaller number, 4590 for the maximum 32-bit signed int. 7745287Sbostic */ 7845427Sbostic #define NBUCKETS (UCHAR_MAX + 1) 7945427Sbostic 8045287Sbostic typedef struct _stack { 8145287Sbostic u_char **bot; 8245287Sbostic int indx, nmemb; 8345287Sbostic } CONTEXT; 8445287Sbostic 8545287Sbostic #define STACKPUSH { \ 8645287Sbostic stackp->bot = p; \ 8745287Sbostic stackp->nmemb = nmemb; \ 8845287Sbostic stackp->indx = indx; \ 8945287Sbostic ++stackp; \ 9045287Sbostic } 9145287Sbostic #define STACKPOP { \ 9245287Sbostic if (stackp == stack) \ 9345287Sbostic break; \ 9445287Sbostic --stackp; \ 9545287Sbostic bot = stackp->bot; \ 9645287Sbostic nmemb = stackp->nmemb; \ 9745287Sbostic indx = stackp->indx; \ 9845287Sbostic } 9945287Sbostic 10045287Sbostic /* 10145287Sbostic * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5, 10245345Sbostic * Ex. 10 and 12. Also, "Three Partition Refinement Algorithms, Paige 10345345Sbostic * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987. 10445287Sbostic * 10545345Sbostic * This uses a simple sort as soon as a bucket crosses a cutoff point, 10645345Sbostic * rather than sorting the entire list after partitioning is finished. 10745345Sbostic * This should be an advantage. 10845287Sbostic * 10945345Sbostic * This is pure MSD instead of LSD of some number of MSD, switching to 11045345Sbostic * the simple sort as soon as possible. Takes linear time relative to 11145345Sbostic * the number of bytes in the strings. 11245287Sbostic */ 11345287Sbostic radixsort(l1, nmemb, tab, endbyte) 11445287Sbostic u_char **l1, *tab, endbyte; 11545287Sbostic register int nmemb; 11645287Sbostic { 11745287Sbostic register int i, indx, t1, t2; 11845287Sbostic register u_char **l2, **p, **bot, *tr; 11945345Sbostic CONTEXT *stack, *stackp; 12045427Sbostic int c[NBUCKETS + 1], max; 12145427Sbostic u_char ltab[NBUCKETS]; 12245287Sbostic 12345287Sbostic if (nmemb <= 1) 12445287Sbostic return(0); 12545287Sbostic 12645346Sbostic /* 12745345Sbostic * T1 is the constant part of the equation, the number of elements 12845345Sbostic * represented on the stack between the top and bottom entries. 12945346Sbostic * It doesn't get rounded as the divide by 2 rounds down (correct 13045346Sbostic * for a value being subtracted). T2, the nelem value, has to be 13145346Sbostic * rounded up before each divide because we want an upper bound; 13245346Sbostic * this could overflow if nmemb is the maximum int. 13345346Sbostic */ 13445427Sbostic t1 = ((__rspartition + 1) * (NBUCKETS - 2)) >> 1; 13545427Sbostic for (i = 0, t2 = nmemb; t2 > __rspartition; i += NBUCKETS - 1) 136*45779Sbostic t2 = ((t2 + 1) >> 1) - t1; 13745345Sbostic if (i) { 13845346Sbostic if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 13945345Sbostic return(-1); 14045345Sbostic } else 14145345Sbostic stack = stackp = NULL; 14245345Sbostic 14345287Sbostic /* 14445345Sbostic * There are two arrays, one provided by the user (l1), and the 14545287Sbostic * temporary one (l2). The data is sorted to the temporary stack, 14645287Sbostic * and then copied back. The speedup of using index to determine 14745287Sbostic * which stack the data is on and simply swapping stacks back and 14845287Sbostic * forth, thus avoiding the copy every iteration, turns out to not 14945287Sbostic * be any faster than the current implementation. 15045287Sbostic */ 15145287Sbostic if (!(l2 = (u_char **)malloc(sizeof(u_char *) * nmemb))) 15245287Sbostic return(-1); 15345287Sbostic 15445287Sbostic /* 15545345Sbostic * Tr references a table of sort weights; multiple entries may 15645287Sbostic * map to the same weight; EOS char must have the lowest weight. 15745287Sbostic */ 15845287Sbostic if (tab) 15945287Sbostic tr = tab; 16045287Sbostic else { 16145287Sbostic tr = ltab; 16245287Sbostic for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 16345287Sbostic tr[t1] = t1 + 1; 16445287Sbostic tr[t2] = 0; 16545427Sbostic for (t1 = endbyte + 1; t1 < NBUCKETS; ++t1) 16645287Sbostic tr[t1] = t1; 16745287Sbostic } 16845287Sbostic 16945345Sbostic /* First sort is entire stack */ 17045287Sbostic bot = l1; 17145287Sbostic indx = 0; 17245287Sbostic 17345287Sbostic for (;;) { 17445345Sbostic /* Clear bucket count array */ 17545287Sbostic bzero((char *)c, sizeof(c)); 17645287Sbostic 17745287Sbostic /* 17845345Sbostic * Compute number of items that sort to the same bucket 17945287Sbostic * for this index. 18045287Sbostic */ 18145287Sbostic for (p = bot, i = nmemb; i--;) 18245287Sbostic ++c[tr[(*p++)[indx]]]; 18345287Sbostic 18445287Sbostic /* 18545345Sbostic * Sum the number of characters into c, dividing the temp 18645287Sbostic * stack into the right number of buckets for this bucket, 18745287Sbostic * this index. C contains the cumulative total of keys 18845287Sbostic * before and included in this bucket, and will later be 18945427Sbostic * used as an index to the bucket. c[NBUCKETS] contains 19045287Sbostic * the total number of elements, for determining how many 19145345Sbostic * elements the last bucket contains. At the same time 19245427Sbostic * find the largest bucket so it gets pushed first. 19345287Sbostic */ 19445427Sbostic for (i = max = t1 = 0, t2 = __rspartition; i <= NBUCKETS; ++i) { 19545427Sbostic if (c[i] > t2) { 19645427Sbostic t2 = c[i]; 19745345Sbostic max = i; 19845345Sbostic } 19945427Sbostic t1 = c[i] += t1; 20045345Sbostic } 20145287Sbostic 20245287Sbostic /* 20345427Sbostic * Partition the elements into buckets; c decrements through 20445427Sbostic * the bucket, and ends up pointing to the first element of 20545427Sbostic * the bucket. 20645287Sbostic */ 20745287Sbostic for (i = nmemb; i--;) { 20845287Sbostic --p; 20945287Sbostic l2[--c[tr[(*p)[indx]]]] = *p; 21045287Sbostic } 21145287Sbostic 21245345Sbostic /* Copy the partitioned elements back to user stack */ 21345287Sbostic bcopy(l2, bot, nmemb * sizeof(u_char *)); 21445287Sbostic 21545287Sbostic ++indx; 21645287Sbostic /* 21745345Sbostic * Sort buckets as necessary; don't sort c[0], it's the 21845287Sbostic * EOS character bucket, and nothing can follow EOS. 21945287Sbostic */ 22045345Sbostic for (i = max; i; --i) { 22145287Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 22245287Sbostic continue; 22345287Sbostic p = bot + t1; 22445287Sbostic if (nmemb > __rspartition) 22545287Sbostic STACKPUSH 22645287Sbostic else 22745287Sbostic SHELLSORT 22845287Sbostic } 22945427Sbostic for (i = max + 1; i < NBUCKETS; ++i) { 23045345Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 23145345Sbostic continue; 23245345Sbostic p = bot + t1; 23345345Sbostic if (nmemb > __rspartition) 23445345Sbostic STACKPUSH 23545345Sbostic else 23645345Sbostic SHELLSORT 23745345Sbostic } 23845345Sbostic /* Break out when stack is empty */ 23945287Sbostic STACKPOP 24045287Sbostic } 24145287Sbostic 24245287Sbostic free((char *)l2); 24345287Sbostic free((char *)stack); 24445287Sbostic return(0); 24545287Sbostic } 246