Permalink
Browse files

added another radix_sort from gorset/radix

  • Loading branch information...
1 parent 3c4d567 commit 0297736779d48aeab1e307755e5ed5b6f73e6bb4 @lh3 lh3 committed Jun 10, 2012
Showing with 41 additions and 0 deletions.
  1. +41 −0 test/ksort_test.cc
View
@@ -670,6 +670,45 @@ void rs_sort2(rstype_t *beg, rstype_t *end, int n_bits, int s)
}
}
*/
+void radix_sort(unsigned *array, int offset, int end, int shift) {
+ int x, y, value, temp;
+ int last[256] = { 0 }, pointer[256];
+
+ for (x=offset; x<end; ++x) {
+ ++last[(array[x] >> shift) & 0xFF];
+ }
+
+ last[0] += offset;
+ pointer[0] = offset;
+ for (x=1; x<256; ++x) {
+ pointer[x] = last[x-1];
+ last[x] += last[x-1];
+ }
+
+ for (x=0; x<256; ++x) {
+ while (pointer[x] != last[x]) {
+ value = array[pointer[x]];
+ y = (value >> shift) & 0xFF;
+ while (x != y) {
+ temp = array[pointer[y]];
+ array[pointer[y]++] = value;
+ value = temp;
+ y = (value >> shift) & 0xFF;
+ }
+ array[pointer[x]++] = value;
+ }
+ }
+
+ if (shift > 0) {
+ shift -= 8;
+ for (x=0; x<256; ++x) {
+ temp = x > 0 ? pointer[x] - pointer[x-1] : pointer[0] - offset;
+ if (temp > 64) {
+ radix_sort(array, pointer[x] - temp, pointer[x], shift);
+ } else if (temp > 1) rs_insertsort(array + pointer[x] - temp, array + pointer[x]);
+ }
+ }
+}
/*************************
*** END OF RADIX SORT ***
*************************/
@@ -785,6 +824,7 @@ int main(int argc, char *argv[])
for (i = 0; i < N; ++i) array[i] = (int)lrand48();
t1 = clock();
RadixSortInPlace_HybridUnsigned_Radix256((unsigned*)array, N);
+// radix_sort((unsigned*)array, 0, N, 24);
t2 = clock();
fprintf(stderr, "vd's radix sort: %.3lf\n", (double)(t2-t1)/CLOCKS_PER_SEC);
for (i = 0; i < N-1; ++i) {
@@ -795,6 +835,7 @@ int main(int argc, char *argv[])
}
t1 = clock();
RadixSortInPlace_HybridUnsigned_Radix256((unsigned*)array, N);
+// radix_sort((unsigned*)array, 0, N, 24);
t2 = clock();
fprintf(stderr, "vd's radix sort (sorted): %.3lf\n", (double)(t2-t1)/CLOCKS_PER_SEC);

0 comments on commit 0297736

Please sign in to comment.