Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ctime>
- #include <cstdlib>
- #include <iostream>
- const int bitsword = 32;
- const int bitsbyte = 8 ;
- const int bytesword = bitsword/bitsbyte;
- const int R = 1 << bitsbyte;
- const int maxN = 100;
- inline int digit(long A, int B) { return (A >> bitsbyte * (bytesword - B - 1)) & (R - 1); }
- template <class Item>
- void radixLSD(Item a[], int l, int r, int maxN) {
- Item *aux = new Item[maxN];
- for (int d = bytesword-1; d >= 0; d--) {
- int i, j, count[R+1];
- for (j = 0; j < R; j++) count[j] = 0;
- for (i = l; i <= r; i++) count[digit(a[ i], d) + 1]++;
- for (j = 1; j < R; j++) count[j] += count[j-1];
- for (i = l; i <= r; i++) aux[count[digit(a[ i], d)]++] = a[ i];
- for (i = l; i <= r; i++) a[ i] = aux[i-l];
- }
- delete[] aux;
- }
- int main() {
- int a[10];
- srand(time(0));
- for (int i=0; i<10; i++) {
- a[ i] = rand() % 100;
- std::cout << a[ i] << " ";
- }
- std::cout << std::endl;
- radixLSD (a, 0, 9, 10);
- for (int i=0; i<10; i++) std::cout << a[ i] << " ";
- std::cout << std::endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment