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*45345Sbostic static char sccsid[] = "@(#)radixsort.c 5.2 (Berkeley) 10/12/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 /* 20*45345Sbostic * 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 /* 54*45345Sbostic * Stackp points to context structures, where each structure schedules a 55*45345Sbostic * partitioning. Radixsort exits when the stack is empty. 5645287Sbostic * 57*45345Sbostic * If the buckets are placed on the stack randomly, the worst case is when: 58*45345Sbostic * 59*45345Sbostic * (nbuckets - 1) contain (npartitions + 1) elements, with the last 60*45345Sbostic * bucket containing (nelements - ((npartitions + 1) * (nbuckets - 1)) 61*45345Sbostic * keys. 62*45345Sbostic * 63*45345Sbostic * In this case, stack growth is bounded by: 64*45345Sbostic * 65*45345Sbostic * (nelements / (npartitions + 1)) - 1 66*45345Sbostic * 67*45345Sbostic * Therefore, we force the largest bucket to be pushed on the stack first. 68*45345Sbostic * Then the worst case is when: 69*45345Sbostic * 70*45345Sbostic * (nbuckets - 2) buckets contain (npartitions + 1) elements, with 71*45345Sbostic * the remaining elements split equally between the first bucket 72*45345Sbostic * pushed and the last bucket pushed. 73*45345Sbostic * 74*45345Sbostic * In this case, stack growth is bounded when: 75*45345Sbostic * 76*45345Sbostic * for (partition_cnt = 0; nelements > npartitions; ++partition_cnt) 77*45345Sbostic * nelements = 78*45345Sbostic * (nelements - (npartitions + 1) * (nbuckets - 2)) / 2; 79*45345Sbostic * The bound is: 80*45345Sbostic * 81*45345Sbostic * limit = partition_cnt * (nbuckets - 1); 8245287Sbostic */ 8345287Sbostic typedef struct _stack { 8445287Sbostic u_char **bot; 8545287Sbostic int indx, nmemb; 8645287Sbostic } CONTEXT; 8745287Sbostic 8845287Sbostic #define STACKPUSH { \ 8945287Sbostic stackp->bot = p; \ 9045287Sbostic stackp->nmemb = nmemb; \ 9145287Sbostic stackp->indx = indx; \ 9245287Sbostic ++stackp; \ 9345287Sbostic } 9445287Sbostic 9545287Sbostic #define STACKPOP { \ 9645287Sbostic if (stackp == stack) \ 9745287Sbostic break; \ 9845287Sbostic --stackp; \ 9945287Sbostic bot = stackp->bot; \ 10045287Sbostic nmemb = stackp->nmemb; \ 10145287Sbostic indx = stackp->indx; \ 10245287Sbostic } 10345287Sbostic 10445287Sbostic /* 10545287Sbostic * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5, 106*45345Sbostic * Ex. 10 and 12. Also, "Three Partition Refinement Algorithms, Paige 107*45345Sbostic * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987. 10845287Sbostic * 109*45345Sbostic * This uses a simple sort as soon as a bucket crosses a cutoff point, 110*45345Sbostic * rather than sorting the entire list after partitioning is finished. 111*45345Sbostic * This should be an advantage. 11245287Sbostic * 113*45345Sbostic * This is pure MSD instead of LSD of some number of MSD, switching to 114*45345Sbostic * the simple sort as soon as possible. Takes linear time relative to 115*45345Sbostic * the number of bytes in the strings. 11645287Sbostic */ 11745287Sbostic radixsort(l1, nmemb, tab, endbyte) 11845287Sbostic u_char **l1, *tab, endbyte; 11945287Sbostic register int nmemb; 12045287Sbostic { 12145287Sbostic register int i, indx, t1, t2; 12245287Sbostic register u_char **l2, **p, **bot, *tr; 123*45345Sbostic CONTEXT *stack, *stackp; 124*45345Sbostic int c[NCHARS + 1], max; 12545287Sbostic u_char ltab[NCHARS]; 12645287Sbostic 12745287Sbostic if (nmemb <= 1) 12845287Sbostic return(0); 12945287Sbostic 130*45345Sbostic /* 131*45345Sbostic * T1 is the constant part of the equation, the number of elements 132*45345Sbostic * represented on the stack between the top and bottom entries. 133*45345Sbostic * Don't round as the divide by 2 rounds down (correct for value 134*45345Sbostic * being subtracted). The nelem value has to be rounded up before 135*45345Sbostic * each divide because we want an upper bound. 136*45345Sbostic */ 137*45345Sbostic t1 = ((__rspartition + 1) * (UCHAR_MAX - 2)) >> 1; 138*45345Sbostic for (i = 0, t2 = nmemb; t2 > __rspartition; i += UCHAR_MAX - 1) 139*45345Sbostic t2 = (++t2 >> 1) - t1; 140*45345Sbostic if (i) { 141*45345Sbostic if (!(stack = stackp = 142*45345Sbostic (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 143*45345Sbostic return(-1); 144*45345Sbostic } else 145*45345Sbostic stack = stackp = NULL; 146*45345Sbostic 14745287Sbostic /* 148*45345Sbostic * There are two arrays, one provided by the user (l1), and the 14945287Sbostic * temporary one (l2). The data is sorted to the temporary stack, 15045287Sbostic * and then copied back. The speedup of using index to determine 15145287Sbostic * which stack the data is on and simply swapping stacks back and 15245287Sbostic * forth, thus avoiding the copy every iteration, turns out to not 15345287Sbostic * be any faster than the current implementation. 15445287Sbostic */ 15545287Sbostic if (!(l2 = (u_char **)malloc(sizeof(u_char *) * nmemb))) 15645287Sbostic return(-1); 15745287Sbostic 15845287Sbostic /* 159*45345Sbostic * Tr references a table of sort weights; multiple entries may 16045287Sbostic * map to the same weight; EOS char must have the lowest weight. 16145287Sbostic */ 16245287Sbostic if (tab) 16345287Sbostic tr = tab; 16445287Sbostic else { 16545287Sbostic tr = ltab; 16645287Sbostic for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 16745287Sbostic tr[t1] = t1 + 1; 16845287Sbostic tr[t2] = 0; 16945287Sbostic for (t1 = endbyte + 1; t1 < NCHARS; ++t1) 17045287Sbostic tr[t1] = t1; 17145287Sbostic } 17245287Sbostic 173*45345Sbostic /* First sort is entire stack */ 17445287Sbostic bot = l1; 17545287Sbostic indx = 0; 17645287Sbostic 17745287Sbostic for (;;) { 178*45345Sbostic /* Clear bucket count array */ 17945287Sbostic bzero((char *)c, sizeof(c)); 18045287Sbostic 18145287Sbostic /* 182*45345Sbostic * Compute number of items that sort to the same bucket 18345287Sbostic * for this index. 18445287Sbostic */ 18545287Sbostic for (p = bot, i = nmemb; i--;) 18645287Sbostic ++c[tr[(*p++)[indx]]]; 18745287Sbostic 18845287Sbostic /* 189*45345Sbostic * Sum the number of characters into c, dividing the temp 19045287Sbostic * stack into the right number of buckets for this bucket, 19145287Sbostic * this index. C contains the cumulative total of keys 19245287Sbostic * before and included in this bucket, and will later be 19345287Sbostic * used as an index to the bucket. c[NCHARS] contains 19445287Sbostic * the total number of elements, for determining how many 195*45345Sbostic * elements the last bucket contains. At the same time 196*45345Sbostic * find the largest bucket so it gets handled first. 19745287Sbostic */ 198*45345Sbostic for (i = 1, t2 = -1; i <= NCHARS; ++i) { 199*45345Sbostic if ((t1 = c[i - 1]) > t2) { 200*45345Sbostic t2 = t1; 201*45345Sbostic max = i; 202*45345Sbostic } 203*45345Sbostic c[i] += t1; 204*45345Sbostic } 20545287Sbostic 20645287Sbostic /* 207*45345Sbostic * Partition the elements into buckets; c decrements 20845287Sbostic * through the bucket, and ends up pointing to the 20945287Sbostic * first element of the bucket. 21045287Sbostic */ 21145287Sbostic for (i = nmemb; i--;) { 21245287Sbostic --p; 21345287Sbostic l2[--c[tr[(*p)[indx]]]] = *p; 21445287Sbostic } 21545287Sbostic 216*45345Sbostic /* Copy the partitioned elements back to user stack */ 21745287Sbostic bcopy(l2, bot, nmemb * sizeof(u_char *)); 21845287Sbostic 21945287Sbostic ++indx; 22045287Sbostic /* 221*45345Sbostic * Sort buckets as necessary; don't sort c[0], it's the 22245287Sbostic * EOS character bucket, and nothing can follow EOS. 22345287Sbostic */ 224*45345Sbostic for (i = max; i; --i) { 22545287Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 22645287Sbostic continue; 22745287Sbostic p = bot + t1; 22845287Sbostic if (nmemb > __rspartition) 22945287Sbostic STACKPUSH 23045287Sbostic else 23145287Sbostic SHELLSORT 23245287Sbostic } 233*45345Sbostic for (i = max + 1; i < NCHARS; ++i) { 234*45345Sbostic if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 235*45345Sbostic continue; 236*45345Sbostic p = bot + t1; 237*45345Sbostic if (nmemb > __rspartition) 238*45345Sbostic STACKPUSH 239*45345Sbostic else 240*45345Sbostic SHELLSORT 241*45345Sbostic } 242*45345Sbostic /* Break out when stack is empty */ 24345287Sbostic STACKPOP 24445287Sbostic } 24545287Sbostic 24645287Sbostic free((char *)l2); 24745287Sbostic free((char *)stack); 24845287Sbostic return(0); 24945287Sbostic } 250