# HG changeset patch # User deba # Date 1133633743 0 # Node ID eaa5f5b855f75adec3f5e65599d6249eeee6ae8b # Parent 1e386f4047c9e06b4dff8adb0c7200a740b44484 Changed implementation and bug fix diff -r 1e386f4047c9 -r eaa5f5b855f7 lemon/radix_sort.h --- a/lemon/radix_sort.h Fri Dec 02 10:02:40 2005 +0000 +++ b/lemon/radix_sort.h Sat Dec 03 18:15:43 2005 +0000 @@ -254,7 +254,7 @@ template unsigned char valueByte(Value value, int byte) { - return *((unsigned char *)(&value) + byte); + return value >> (std::numeric_limits::digits * byte); } template @@ -365,13 +365,11 @@ try { bool dir = true; std::copy(first, last, buffer); - for (int i = 0; i < sizeof(Value); ++i) { + for (int i = 0; i < (int)sizeof(Value); ++i) { if (dir) { - counterIntroSort(buffer, buffer + length, buffer + length, - i, functor); + counterIntroSort(buffer, buffer + length, buffer + length, i, functor); } else { - counterIntroSort(buffer + length, buffer + 2 * length, buffer, - i, functor); + counterIntroSort(buffer + length, buffer + 2 * length, buffer, i, functor); } dir = !dir; } @@ -425,8 +423,8 @@ /// By the occurence number it is possible to copy the container /// in the right order in \c O(n) time. The algorithm sorts the container /// by each bytes in forward direction which sorts the container by the - /// whole value. This way, let be - /// \c c the maximal capacity and \c n the number of the items in + /// whole value. This way, let be \c c the maximal capacity of the integer + /// type and \c n the number of the items in /// the container, the time complexity of the algorithm \c O(log(c)*n) /// and the additional space complexity is \c O(n). /// diff -r 1e386f4047c9 -r eaa5f5b855f7 test/radix_sort_test.cc --- a/test/radix_sort_test.cc Fri Dec 02 10:02:40 2005 +0000 +++ b/test/radix_sort_test.cc Sat Dec 03 18:15:43 2005 +0000 @@ -15,29 +15,55 @@ using namespace lemon; void checkRadixSort() { - int n = 10000; - vector data1(n), data2(n); - for (int i = 0; i < n; ++i) { - data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500; - } - radixSort(data1.begin(), data1.end()); - sort(data2.begin(), data2.end()); - for (int i = 0; i < n; ++i) { - check(data1[i] == data2[i], "Test failed"); + { + int n = 10000; + vector data1(n), data2(n); + for (int i = 0; i < n; ++i) { + data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500; + } + radixSort(data1.begin(), data1.end()); + sort(data2.begin(), data2.end()); + for (int i = 0; i < n; ++i) { + check(data1[i] == data2[i], "Test failed"); + } + } { + int n = 10000; + vector data1(n), data2(n); + for (int i = 0; i < n; ++i) { + data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0))); + } + radixSort(data1.begin(), data1.end()); + sort(data2.begin(), data2.end()); + for (int i = 0; i < n; ++i) { + check(data1[i] == data2[i], "Test failed"); + } } } void checkCounterSort() { - int n = 10000; - vector data1(n), data2(n); - for (int i = 0; i < n; ++i) { - data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500; - } - counterSort(data1.begin(), data1.end()); - sort(data2.begin(), data2.end()); - for (int i = 0; i < n; ++i) { - check(data1[i] == data2[i], "Test failed"); + { + int n = 10000; + vector data1(n), data2(n); + for (int i = 0; i < n; ++i) { + data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500; + } + counterSort(data1.begin(), data1.end()); + sort(data2.begin(), data2.end()); + for (int i = 0; i < n; ++i) { + check(data1[i] == data2[i], "Test failed"); + } + } { + int n = 10000; + vector data1(n), data2(n); + for (int i = 0; i < n; ++i) { + data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0))); + } + counterSort(data1.begin(), data1.end()); + sort(data2.begin(), data2.end()); + for (int i = 0; i < n; ++i) { + check(data1[i] == data2[i], "Test failed"); + } } }