标签:++i bit ifd sort size typedef radix -- bitset
这个是二进制版本的,或许输出成bitset会好一些。这个MAXK不太清楚应该是怎么样做的,到时候具体分析移位之后的结果吧?
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXK = 28;
const int K = 4;
const int BASE = 1 << K;
const int BASEMO = BASE - 1;
int base[BASE];
int a[1005];
int tmp[1005];
void radix_sort(int *pb, int *pe) {
int n = pe - pb;
for(int k = 0; k < MAXK; k += K) {
for(int i = 0; i < n; ++i)
printf(" %d", pb[i]);
puts("");
memset(base, 0, sizeof(base));
for(int i = 0; i < n; ++i)
base[(pb[i] >> k) & (BASEMO)]++;
for(int i = 1; i < BASE; ++i)
base[i] += base[i - 1];
for(int i = n - 1; i >= 0; --i)
tmp[--base[(pb[i] >> k) & (BASEMO)]] = pb[i];
for(int i = n - 1; i >= 0; --i)
pb[i] = tmp[i];
}
for(int i = 0; i < n; ++i)
printf(" %d", pb[i]);
puts("");
}
int main() {
#ifdef Inko
freopen("Inko.in", "r", stdin);
#endif // Inko
int n = 30;
for(int i = 1; i <= n; ++i)
a[i] = rand()%1024;
radix_sort(a + 1, a + 1 + n);
}
标签:++i bit ifd sort size typedef radix -- bitset
原文地址:https://www.cnblogs.com/Inko/p/11380532.html