Changed implementation and bug fix
authordeba
Sat, 03 Dec 2005 18:15:43 +0000
changeset 1844eaa5f5b855f7
parent 1843 1e386f4047c9
child 1845 f8bbfed86036
Changed implementation and bug fix
lemon/radix_sort.h
test/radix_sort_test.cc
     1.1 --- a/lemon/radix_sort.h	Fri Dec 02 10:02:40 2005 +0000
     1.2 +++ b/lemon/radix_sort.h	Sat Dec 03 18:15:43 2005 +0000
     1.3 @@ -254,7 +254,7 @@
     1.4  
     1.5    template <typename Value>
     1.6    unsigned char valueByte(Value value, int byte) {
     1.7 -    return *((unsigned char *)(&value) + byte);
     1.8 +    return value >> (std::numeric_limits<unsigned char>::digits * byte);
     1.9    }
    1.10  
    1.11    template <typename Functor, typename Key>
    1.12 @@ -365,13 +365,11 @@
    1.13      try {
    1.14        bool dir = true;
    1.15        std::copy(first, last, buffer);
    1.16 -      for (int i = 0; i < sizeof(Value); ++i) {
    1.17 +      for (int i = 0; i < (int)sizeof(Value); ++i) {
    1.18  	if (dir) {
    1.19 -	  counterIntroSort(buffer, buffer + length, buffer + length, 
    1.20 -			   i, functor);
    1.21 +	  counterIntroSort(buffer, buffer + length, buffer + length, i, functor);
    1.22  	} else {
    1.23 -	  counterIntroSort(buffer + length, buffer + 2 * length, buffer, 
    1.24 -			   i, functor);
    1.25 +	  counterIntroSort(buffer + length, buffer + 2 * length, buffer, i, functor);
    1.26  	}
    1.27  	dir = !dir;
    1.28        }
    1.29 @@ -425,8 +423,8 @@
    1.30    /// By the occurence number it is possible to copy the container
    1.31    /// in the right order in \c O(n) time. The algorithm sorts the container
    1.32    /// by each bytes in forward direction which sorts the container by the
    1.33 -  /// whole value. This way, let be
    1.34 -  /// \c c the maximal capacity and \c n the number of the items in
    1.35 +  /// whole value. This way, let be \c c the maximal capacity of the integer 
    1.36 +  /// type and \c n the number of the items in
    1.37    /// the container, the time complexity of the algorithm \c O(log(c)*n)
    1.38    /// and the additional space complexity is \c O(n).
    1.39    ///
     2.1 --- a/test/radix_sort_test.cc	Fri Dec 02 10:02:40 2005 +0000
     2.2 +++ b/test/radix_sort_test.cc	Sat Dec 03 18:15:43 2005 +0000
     2.3 @@ -15,29 +15,55 @@
     2.4  using namespace lemon;
     2.5  
     2.6  void checkRadixSort() {
     2.7 -  int n = 10000;
     2.8 -  vector<int> data1(n), data2(n);
     2.9 -  for (int i = 0; i < n; ++i) {
    2.10 -    data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
    2.11 -  }
    2.12 -  radixSort(data1.begin(), data1.end());
    2.13 -  sort(data2.begin(), data2.end());
    2.14 -  for (int i = 0; i < n; ++i) {
    2.15 -    check(data1[i] == data2[i], "Test failed");
    2.16 +  {
    2.17 +    int n = 10000;
    2.18 +    vector<int> data1(n), data2(n);
    2.19 +    for (int i = 0; i < n; ++i) {
    2.20 +      data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
    2.21 +    }
    2.22 +    radixSort(data1.begin(), data1.end());
    2.23 +    sort(data2.begin(), data2.end());
    2.24 +    for (int i = 0; i < n; ++i) {
    2.25 +      check(data1[i] == data2[i], "Test failed");
    2.26 +    }
    2.27 +  } {
    2.28 +    int n = 10000;
    2.29 +    vector<unsigned char> data1(n), data2(n);
    2.30 +    for (int i = 0; i < n; ++i) {
    2.31 +      data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0)));
    2.32 +    }
    2.33 +    radixSort(data1.begin(), data1.end());
    2.34 +    sort(data2.begin(), data2.end());
    2.35 +    for (int i = 0; i < n; ++i) {
    2.36 +      check(data1[i] == data2[i], "Test failed");
    2.37 +    }
    2.38    }
    2.39  }
    2.40  
    2.41  
    2.42  void checkCounterSort() {
    2.43 -  int n = 10000;
    2.44 -  vector<int> data1(n), data2(n);
    2.45 -  for (int i = 0; i < n; ++i) {
    2.46 -    data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
    2.47 -  }
    2.48 -  counterSort(data1.begin(), data1.end());
    2.49 -  sort(data2.begin(), data2.end());
    2.50 -  for (int i = 0; i < n; ++i) {
    2.51 -    check(data1[i] == data2[i], "Test failed");
    2.52 +  {
    2.53 +    int n = 10000;
    2.54 +    vector<int> data1(n), data2(n);
    2.55 +    for (int i = 0; i < n; ++i) {
    2.56 +      data1[i] = data2[i] = (int)(1000 * (rand() / (RAND_MAX + 1.0))) - 500;
    2.57 +    }
    2.58 +    counterSort(data1.begin(), data1.end());
    2.59 +    sort(data2.begin(), data2.end());
    2.60 +    for (int i = 0; i < n; ++i) {
    2.61 +      check(data1[i] == data2[i], "Test failed");
    2.62 +    } 
    2.63 +  } {
    2.64 +    int n = 10000;
    2.65 +    vector<unsigned char> data1(n), data2(n);
    2.66 +    for (int i = 0; i < n; ++i) {
    2.67 +      data1[i] = data2[i] = (int)(200 * (rand() / (RAND_MAX + 1.0)));
    2.68 +    }
    2.69 +    counterSort(data1.begin(), data1.end());
    2.70 +    sort(data2.begin(), data2.end());
    2.71 +    for (int i = 0; i < n; ++i) {
    2.72 +      check(data1[i] == data2[i], "Test failed");
    2.73 +    } 
    2.74    }
    2.75  }
    2.76