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*45346Sbostic static char sccsid[] = "@(#)radixsort.c 5.3 (Berkeley) 10/13/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 #define NCHARS (UCHAR_MAX + 1) 1845287Sbostic 1945287Sbostic /* 2045345Sbostic * Shellsort (diminishing increment sort) from Data Structures and 2145287Sbostic * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 2245287Sbostic * see also Knuth Vol. 3, page 84. The increments are selected from 2345287Sbostic * formula (8), page 95. Roughly O(N^3/2). 2445287Sbostic * 2545287Sbostic * __rspartition is the cutoff point for a further partitioning instead 2645287Sbostic * of a shellsort. If it changes check __rsshell_increments. Both of 2745287Sbostic * these are exported, as the best values are data dependent. Unrolling 2845287Sbostic * this loop has not proven worthwhile. 2945287Sbostic */ 3045287Sbostic #define NPARTITION 40 3145287Sbostic int __rspartition = NPARTITION; 3245287Sbostic int __rsshell_increments[] = { 4, 1, 0, 0, 0, 0, 0, 0 }; 3345287Sbostic #define SHELLSORT { \ 3445287Sbostic register u_char ch, *s1, *s2; \ 3545287Sbostic register int incr, *incrp; \ 3645287Sbostic for (incrp = __rsshell_increments; incr = *incrp++;) \ 3745287Sbostic for (t1 = incr; t1 < nmemb; ++t1) \ 3845287Sbostic for (t2 = t1 - incr; t2 >= 0;) { \ 3945287Sbostic s1 = p[t2] + indx; \ 4045287Sbostic s2 = p[t2 + incr] + indx; \ 4145287Sbostic while ((ch = tr[*s1++]) == tr[*s2] && ch) \ 4245287Sbostic ++s2; \ 4345287Sbostic if (ch > tr[*s2]) { \ 4445287Sbostic s1 = p[t2]; \ 4545287Sbostic p[t2] = p[t2 + incr]; \ 4645287Sbostic p[t2 + incr] = s1; \ 4745287Sbostic t2 -= incr; \ 4845287Sbostic } else \ 4945287Sbostic break; \ 5045287Sbostic } \ 5145287Sbostic } 5245287Sbostic 5345287Sbostic /* 5445345Sbostic * Stackp points to context structures, where each structure schedules a 5545345Sbostic * partitioning. Radixsort exits when the stack is empty. 5645287Sbostic * 57*45346Sbostic * If the buckets are placed on the stack randomly, the worst case is when 58*45346Sbostic * all the buckets but one contain (NPARTITION + 1) elements and the bucket 59*45346Sbostic * pushed on the stack last contains the rest of the elements. In this case, 60*45346Sbostic * stack growth is bounded by: 6145345Sbostic * 6245345Sbostic * (nelements / (npartitions + 1)) - 1 6345345Sbostic * 64*45346Sbostic * This is a very large number. By forcing the largest bucket to be pushed 65*45346Sbostic * on the stack first the worst case is when all but two buckets each contain 66*45346Sbostic * (NPARTITION + 1) elements, with the remaining elements split equally between 67*45346Sbostic * the first and last buckets pushed on the stack. In this case, stack growth 68*45346Sbostic * is bounded when: 6945345Sbostic * 70*45346Sbostic * for (partition_cnt = 0; nelements > npartitions; ++partition_cnt) 7145345Sbostic * nelements = 7245345Sbostic * (nelements - (npartitions + 1) * (nbuckets - 2)) / 2; 7345345Sbostic * The bound is: 7445345Sbostic * 7545345Sbostic * limit = partition_cnt * (nbuckets - 1); 76*45346Sbostic * 77*45346Sbostic * This is a much smaller number. 7845287Sbostic */ 7945287Sbostic typedef struct _stack { 8045287Sbostic u_char **bot; 8145287Sbostic int indx, nmemb; 8245287Sbostic } CONTEXT; 8345287Sbostic 8445287Sbostic #define STACKPUSH { \ 8545287Sbostic stackp->bot = p; \ 8645287Sbostic stackp->nmemb = nmemb; \ 8745287Sbostic stackp->indx = indx; \ 8845287Sbostic ++stackp; \ 8945287Sbostic } 9045287Sbostic #define STACKPOP { \ 9145287Sbostic if (stackp == stack) \ 9245287Sbostic break; \ 9345287Sbostic --stackp; \ 9445287Sbostic bot = stackp->bot; \ 9545287Sbostic nmemb = stackp->nmemb; \ 9645287Sbostic indx = stackp->indx; \ 9745287Sbostic } 9845287Sbostic 9945287Sbostic /* 10045287Sbostic * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5, 10145345Sbostic * Ex. 10 and 12. Also, "Three Partition Refinement Algorithms, Paige 10245345Sbostic * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987. 10345287Sbostic * 10445345Sbostic * This uses a simple sort as soon as a bucket crosses a cutoff point, 10545345Sbostic * rather than sorting the entire list after partitioning is finished. 10645345Sbostic * This should be an advantage. 10745287Sbostic * 10845345Sbostic * This is pure MSD instead of LSD of some number of MSD, switching to 10945345Sbostic * the simple sort as soon as possible. Takes linear time relative to 11045345Sbostic * the number of bytes in the strings. 11145287Sbostic */ 11245287Sbostic radixsort(l1, nmemb, tab, endbyte) 11345287Sbostic u_char **l1, *tab, endbyte; 11445287Sbostic register int nmemb; 11545287Sbostic { 11645287Sbostic register int i, indx, t1, t2; 11745287Sbostic register u_char **l2, **p, **bot, *tr; 11845345Sbostic CONTEXT *stack, *stackp; 11945345Sbostic int c[NCHARS + 1], max; 12045287Sbostic u_char ltab[NCHARS]; 12145287Sbostic 12245287Sbostic if (nmemb <= 1) 12345287Sbostic return(0); 12445287Sbostic 125*45346Sbostic /* 12645345Sbostic * T1 is the constant part of the equation, the number of elements 12745345Sbostic * represented on the stack between the top and bottom entries. 128*45346Sbostic * It doesn't get rounded as the divide by 2 rounds down (correct 129*45346Sbostic * for a value being subtracted). T2, the nelem value, has to be 130*45346Sbostic * rounded up before each divide because we want an upper bound; 131*45346Sbostic * this could overflow if nmemb is the maximum int. 132*45346Sbostic */ 13345345Sbostic t1 = ((__rspartition + 1) * (UCHAR_MAX - 2)) >> 1; 134*45346Sbostic for (i = 0, t2 = nmemb; t2 > __rspartition; i += UCHAR_MAX - 1) 13545345Sbostic t2 = (++t2 >> 1) - t1; 13645345Sbostic if (i) { 137*45346Sbostic if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 13845345Sbostic return(-1); 13945345Sbostic } else 14045345Sbostic stack = stackp = NULL; 14145345Sbostic 14245287Sbostic /* 14345345Sbostic * There are two arrays, one provided by the user (l1), and the 14445287Sbostic * temporary one (l2). The data is sorted to the temporary stack, 14545287Sbostic * and then copied back. The speedup of using index to determine 14645287Sbostic * which stack the data is on and simply swapping stacks back and 14745287Sbostic * forth, thus avoiding the copy every iteration, turns out to not 14845287Sbostic * be any faster than the current implementation. 14945287Sbostic */ 15045287Sbostic if (!(l2 = (u_char **)malloc(sizeof(u_char *) * nmemb))) 15145287Sbostic return(-1); 15245287Sbostic 15345287Sbostic /* 15445345Sbostic * Tr references a table of sort weights; multiple entries may 15545287Sbostic * map to the same weight; EOS char must have the lowest weight. 15645287Sbostic */ 15745287Sbostic if (tab) 15845287Sbostic tr = tab; 15945287Sbostic else { 16045287Sbostic tr = ltab; 16145287Sbostic for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 16245287Sbostic tr[t1] = t1 + 1; 16345287Sbostic tr[t2] = 0; 16445287Sbostic for (t1 = endbyte + 1; t1 < NCHARS; ++t1) 16545287Sbostic tr[t1] = t1; 16645287Sbostic } 16745287Sbostic 16845345Sbostic /* First sort is entire stack */ 16945287Sbostic bot = l1; 17045287Sbostic indx = 0; 17145287Sbostic 17245287Sbostic for (;;) { 17345345Sbostic /* Clear bucket count array */ 17445287Sbostic bzero((char *)c, sizeof(c)); 17545287Sbostic 17645287Sbostic /* 17745345Sbostic * Compute number of items that sort to the same bucket 17845287Sbostic * for this index. 17945287Sbostic */ 18045287Sbostic for (p = bot, i = nmemb; i--;) 18145287Sbostic ++c[tr[(*p++)[indx]]]; 18245287Sbostic 18345287Sbostic /* 18445345Sbostic * Sum the number of characters into c, dividing the temp 18545287Sbostic * stack into the right number of buckets for this bucket, 18645287Sbostic * this index. C contains the cumulative total of keys 18745287Sbostic * before and included in this bucket, and will later be 18845287Sbostic * used as an index to the bucket. c[NCHARS] contains 18945287Sbostic * the total number of elements, for determining how many 19045345Sbostic * elements the last bucket contains. At the same time 19145345Sbostic * find the largest bucket so it gets handled first. 19245287Sbostic */ 19345345Sbostic for (i = 1, t2 = -1; i <= NCHARS; ++i) { 19445345Sbostic if ((t1 = c[i - 1]) > t2) { 19545345Sbostic t2 = t1; 19645345Sbostic max = i; 19745345Sbostic } 19845345Sbostic c[i] += t1; 19945345Sbostic } 20045287Sbostic 20145287Sbostic /* 20245345Sbostic * Partition the elements into buckets; c decrements 20345287Sbostic * through the bucket, and ends up pointing to the 20445287Sbostic * first element of the bucket. 20545287Sbostic */ 20645287Sbostic for (i = nmemb; i--;) { 20745287Sbostic --p; 20845287Sbostic l2[--c[tr[(*p)[indx]]]] = *p; 20945287Sbostic } 21045287Sbostic 21145345Sbostic /* Copy the partitioned elements back to user stack */ 21245287Sbostic bcopy(l2, bot, nmemb * sizeof(u_char *)); 21345287Sbostic 21445287Sbostic ++indx; 21545287Sbostic /* 21645345Sbostic * Sort buckets as necessary; don't sort c[0], it's the 21745287Sbostic * EOS character bucket, and nothing can follow EOS. 21845287Sbostic */ 21945345Sbostic for (i = max; i; --i) { 22045287Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 22145287Sbostic continue; 22245287Sbostic p = bot + t1; 22345287Sbostic if (nmemb > __rspartition) 22445287Sbostic STACKPUSH 22545287Sbostic else 22645287Sbostic SHELLSORT 22745287Sbostic } 22845345Sbostic for (i = max + 1; i < NCHARS; ++i) { 22945345Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 23045345Sbostic continue; 23145345Sbostic p = bot + t1; 23245345Sbostic if (nmemb > __rspartition) 23345345Sbostic STACKPUSH 23445345Sbostic else 23545345Sbostic SHELLSORT 23645345Sbostic } 23745345Sbostic /* Break out when stack is empty */ 23845287Sbostic STACKPOP 23945287Sbostic } 24045287Sbostic 24145287Sbostic free((char *)l2); 24245287Sbostic free((char *)stack); 24345287Sbostic return(0); 24445287Sbostic } 245