Nếu theo sách ở VN thì kiểu radix sort thường là sắp xếp dựa trên con số, mình có tìm thấy 1 kiểu sắp xếp dựa trên bit, cũng là radi sort, bạn có thể tham khảo thêm, rất hay và ngắn gọn:
Code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
void radix (int byte, long N, long *source, long *dest)
{
long count[256];
long index[256];
memset (count, 0, sizeof (count));
for ( int i=0; i<N; i++ ) count[((source[i])>>(byte*8))&0xff]++;
index[0]=0;
for (int i=1; i<256; i++ ) index[i]=index[i-1]+count[i-1];
for (int i=0; i<N; i++ ) dest[index[((source[i])>>(byte*8))&0xff]++] = source[i];
}
void radixsort (long *source, long *temp, long N)
{
radix (0, N, source, temp);
radix (1, N, temp, source);
radix (2, N, source, temp);
radix (3, N, temp, source);
}
void make_random (long *data, long N)
{
for ( int i=0; i<N; i++ ) data[i]=rand()%32760;
}
long data[10];
long temp[10];
int main()
{
srand(time(0)); make_random(data, 10);
for ( int i=0; i<10; i++ ) cout <<setw(6) << data[i];
cout <<endl;
radixsort (data, temp, 10);
for ( int i=0; i<10; i++ ) cout <<setw(6) << data[i];
getchar(); return 0;
}