From: Heng Li Date: Sun, 10 Jun 2012 22:45:38 +0000 (-0400) Subject: added another radix_sort from gorset/radix X-Git-Tag: spawn-final~46 X-Git-Url: http://www.kaiwu.me/postgresql/commit/static/gitweb.js?a=commitdiff_plain;h=0297736779d48aeab1e307755e5ed5b6f73e6bb4;p=klib.git added another radix_sort from gorset/radix --- diff --git a/test/ksort_test.cc b/test/ksort_test.cc index 75420bd..8950d80 100644 --- a/test/ksort_test.cc +++ b/test/ksort_test.cc @@ -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> 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);