Advertisement
Guest User

NibbleSort-Vetter

a guest
Feb 2nd, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.82 KB | None | 0 0
  1. static const unsigned long counting_table[16 * 16];
  2. static const unsigned long result_table[16 * 16 * 8];
  3. static const unsigned char shifting_table[16 *16];
  4.  
  5. void counting_nibble_sort_lookup(unsigned long* buf)
  6. {
  7.     for (int num = 0; num < 1024; num++) {
  8.         unsigned long value = buf[num];
  9.         unsigned long count = 0;
  10.         for (int i = 0; i < 16; i += 2)
  11.             count += counting_table[(value >> (i * 4)) & 0xFF];
  12.  
  13.     if ((count & (count - 1)) == 0)
  14.             continue;
  15.  
  16.         unsigned long result = 0;
  17.         for (int i = 0; i < 16; i += 2) {
  18.             int double_nibble_count = ((count >> (56 - 4 * i)) & 0xFF);
  19.             result <<= shifting_table[double_nibble_count];
  20.             result |= result_table[double_nibble_count + i * 16 * 8];
  21.         }
  22.         buf[num] = result;
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement