1 /*- 2 * Copyright (c) 1990 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #if defined(LIBC_SCCS) && !defined(lint) 9 static char sccsid[] = "@(#)radixsort.c 5.8 (Berkeley) 07/23/91"; 10 #endif /* LIBC_SCCS and not lint */ 11 12 #include <sys/types.h> 13 #include <limits.h> 14 #include <stdlib.h> 15 #include <stddef.h> 16 #include <string.h> 17 18 /* 19 * __rspartition is the cutoff point for a further partitioning instead 20 * of a shellsort. If it changes check __rsshell_increments. Both of 21 * these are exported, as the best values are data dependent. 22 */ 23 #define NPARTITION 40 24 int __rspartition = NPARTITION; 25 int __rsshell_increments[] = { 4, 1, 0, 0, 0, 0, 0, 0 }; 26 27 /* 28 * Stackp points to context structures, where each structure schedules a 29 * partitioning. Radixsort exits when the stack is empty. 30 * 31 * If the buckets are placed on the stack randomly, the worst case is when 32 * all the buckets but one contain (npartitions + 1) elements and the bucket 33 * pushed on the stack last contains the rest of the elements. In this case, 34 * stack growth is bounded by: 35 * 36 * limit = (nelements / (npartitions + 1)) - 1; 37 * 38 * This is a very large number, 52,377,648 for the maximum 32-bit signed int. 39 * 40 * By forcing the largest bucket to be pushed on the stack first, the worst 41 * case is when all but two buckets each contain (npartitions + 1) elements, 42 * with the remaining elements split equally between the first and last 43 * buckets pushed on the stack. In this case, stack growth is bounded when: 44 * 45 * for (partition_cnt = 0; nelements > npartitions; ++partition_cnt) 46 * nelements = 47 * (nelements - (npartitions + 1) * (nbuckets - 2)) / 2; 48 * The bound is: 49 * 50 * limit = partition_cnt * (nbuckets - 1); 51 * 52 * This is a much smaller number, 4590 for the maximum 32-bit signed int. 53 */ 54 #define NBUCKETS (UCHAR_MAX + 1) 55 56 typedef struct _stack { 57 const u_char **bot; 58 int indx, nmemb; 59 } CONTEXT; 60 61 #define STACKPUSH { \ 62 stackp->bot = p; \ 63 stackp->nmemb = nmemb; \ 64 stackp->indx = indx; \ 65 ++stackp; \ 66 } 67 #define STACKPOP { \ 68 if (stackp == stack) \ 69 break; \ 70 --stackp; \ 71 bot = stackp->bot; \ 72 nmemb = stackp->nmemb; \ 73 indx = stackp->indx; \ 74 } 75 76 /* 77 * A variant of MSD radix sorting; see Knuth Vol. 3, page 177, and 5.2.5, 78 * Ex. 10 and 12. Also, "Three Partition Refinement Algorithms, Paige 79 * and Tarjan, SIAM J. Comput. Vol. 16, No. 6, December 1987. 80 * 81 * This uses a simple sort as soon as a bucket crosses a cutoff point, 82 * rather than sorting the entire list after partitioning is finished. 83 * This should be an advantage. 84 * 85 * This is pure MSD instead of LSD of some number of MSD, switching to 86 * the simple sort as soon as possible. Takes linear time relative to 87 * the number of bytes in the strings. 88 */ 89 int 90 radixsort(l1, nmemb, tab, endbyte) 91 const u_char **l1; 92 register int nmemb; 93 const u_char *tab; 94 u_int endbyte; 95 { 96 register int i, indx, t1, t2; 97 register const u_char **l2; 98 register const u_char **p; 99 register const u_char **bot; 100 register const u_char *tr; 101 CONTEXT *stack, *stackp; 102 int c[NBUCKETS + 1], max; 103 u_char ltab[NBUCKETS]; 104 static void shellsort(); 105 106 if (nmemb <= 1) 107 return(0); 108 109 /* 110 * T1 is the constant part of the equation, the number of elements 111 * represented on the stack between the top and bottom entries. 112 * It doesn't get rounded as the divide by 2 rounds down (correct 113 * for a value being subtracted). T2, the nelem value, has to be 114 * rounded up before each divide because we want an upper bound; 115 * this could overflow if nmemb is the maximum int. 116 */ 117 t1 = ((__rspartition + 1) * (NBUCKETS - 2)) >> 1; 118 for (i = 0, t2 = nmemb; t2 > __rspartition; i += NBUCKETS - 1) 119 t2 = ((t2 + 1) >> 1) - t1; 120 if (i) { 121 if (!(stack = stackp = (CONTEXT *)malloc(i * sizeof(CONTEXT)))) 122 return(-1); 123 } else 124 stack = stackp = NULL; 125 126 /* 127 * There are two arrays, one provided by the user (l1), and the 128 * temporary one (l2). The data is sorted to the temporary stack, 129 * and then copied back. The speedup of using index to determine 130 * which stack the data is on and simply swapping stacks back and 131 * forth, thus avoiding the copy every iteration, turns out to not 132 * be any faster than the current implementation. 133 */ 134 if (!(l2 = (const u_char **)malloc(sizeof(u_char *) * nmemb))) 135 return(-1); 136 137 /* 138 * Tr references a table of sort weights; multiple entries may 139 * map to the same weight; EOS char must have the lowest weight. 140 */ 141 if (tab) 142 tr = tab; 143 else { 144 for (t1 = 0, t2 = endbyte; t1 < t2; ++t1) 145 ltab[t1] = t1 + 1; 146 ltab[t2] = 0; 147 for (t1 = endbyte + 1; t1 < NBUCKETS; ++t1) 148 ltab[t1] = t1; 149 tr = ltab; 150 } 151 152 /* First sort is entire stack */ 153 bot = l1; 154 indx = 0; 155 156 for (;;) { 157 /* Clear bucket count array */ 158 bzero((char *)c, sizeof(c)); 159 160 /* 161 * Compute number of items that sort to the same bucket 162 * for this index. 163 */ 164 for (p = bot, i = nmemb; --i >= 0;) 165 ++c[tr[(*p++)[indx]]]; 166 167 /* 168 * Sum the number of characters into c, dividing the temp 169 * stack into the right number of buckets for this bucket, 170 * this index. C contains the cumulative total of keys 171 * before and included in this bucket, and will later be 172 * used as an index to the bucket. c[NBUCKETS] contains 173 * the total number of elements, for determining how many 174 * elements the last bucket contains. At the same time 175 * find the largest bucket so it gets pushed first. 176 */ 177 for (i = max = t1 = 0, t2 = __rspartition; i <= NBUCKETS; ++i) { 178 if (c[i] > t2) { 179 t2 = c[i]; 180 max = i; 181 } 182 t1 = c[i] += t1; 183 } 184 185 /* 186 * Partition the elements into buckets; c decrements through 187 * the bucket, and ends up pointing to the first element of 188 * the bucket. 189 */ 190 for (i = nmemb; --i >= 0;) { 191 --p; 192 l2[--c[tr[(*p)[indx]]]] = *p; 193 } 194 195 /* Copy the partitioned elements back to user stack */ 196 bcopy(l2, bot, nmemb * sizeof(u_char *)); 197 198 ++indx; 199 /* 200 * Sort buckets as necessary; don't sort c[0], it's the 201 * EOS character bucket, and nothing can follow EOS. 202 */ 203 for (i = max; i; --i) { 204 if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 205 continue; 206 p = bot + t1; 207 if (nmemb > __rspartition) 208 STACKPUSH 209 else 210 shellsort(p, indx, nmemb, tr); 211 } 212 for (i = max + 1; i < NBUCKETS; ++i) { 213 if ((nmemb = c[i + 1] - (t1 = c[i])) < 2) 214 continue; 215 p = bot + t1; 216 if (nmemb > __rspartition) 217 STACKPUSH 218 else 219 shellsort(p, indx, nmemb, tr); 220 } 221 /* Break out when stack is empty */ 222 STACKPOP 223 } 224 225 free((char *)l2); 226 free((char *)stack); 227 return(0); 228 } 229 230 /* 231 * Shellsort (diminishing increment sort) from Data Structures and 232 * Algorithms, Aho, Hopcraft and Ullman, 1983 Edition, page 290; 233 * see also Knuth Vol. 3, page 84. The increments are selected from 234 * formula (8), page 95. Roughly O(N^3/2). 235 */ 236 static void 237 shellsort(p, indx, nmemb, tr) 238 register u_char **p, *tr; 239 register int indx, nmemb; 240 { 241 register u_char ch, *s1, *s2; 242 register int incr, *incrp, t1, t2; 243 244 for (incrp = __rsshell_increments; incr = *incrp++;) 245 for (t1 = incr; t1 < nmemb; ++t1) 246 for (t2 = t1 - incr; t2 >= 0;) { 247 s1 = p[t2] + indx; 248 s2 = p[t2 + incr] + indx; 249 while ((ch = tr[*s1++]) == tr[*s2] && ch) 250 ++s2; 251 if (ch > tr[*s2]) { 252 s1 = p[t2]; 253 p[t2] = p[t2 + incr]; 254 p[t2 + incr] = s1; 255 t2 -= incr; 256 } else 257 break; 258 } 259 } 260