Guest User

Untitled

a guest
May 31st, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. #include <ctime>
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. const int bitsword = 32;
  6. const int bitsbyte = 8 ;
  7. const int bytesword = bitsword/bitsbyte;
  8. const int R = 1 << bitsbyte;
  9. const int maxN = 100;
  10.  
  11. inline int digit(long A, int B) { return (A >> bitsbyte * (bytesword - B - 1)) & (R - 1); }
  12.  
  13. template <class Item>
  14. void radixLSD(Item a[], int l, int r, int maxN) {
  15.     Item *aux = new Item[maxN];
  16.     for (int d = bytesword-1; d >= 0; d--) {
  17.         int i, j, count[R+1];
  18.         for (j = 0; j < R; j++) count[j] = 0;
  19.         for (i = l; i <= r; i++) count[digit(a[ i], d) + 1]++;
  20.         for (j = 1; j < R; j++) count[j] += count[j-1];
  21.         for (i = l; i <= r; i++) aux[count[digit(a[ i], d)]++] = a[ i];
  22.         for (i = l; i <= r; i++) a[ i] = aux[i-l];
  23.     }
  24.     delete[] aux;
  25. }
  26.  
  27. int main() {
  28.     int a[10];
  29.     srand(time(0));
  30.     for (int i=0; i<10; i++) {
  31.         a[ i] = rand() % 100;
  32.         std::cout << a[ i] << " ";
  33.     }
  34.     std::cout << std::endl;
  35.     radixLSD (a, 0, 9, 10);
  36.     for (int i=0; i<10; i++) std::cout << a[ i] << " ";
  37.     std::cout << std::endl;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment