Pagini recente » Cod sursa (job #3162350) | Cod sursa (job #144086) | Cod sursa (job #1159700) | Cod sursa (job #2460547) | Cod sursa (job #3124594)
#include <fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
const int N_MAX = 5e5;
const int MAX_BITS = 32;
const int BITS_PER_STEP = 8;
const int BASE = (1 << BITS_PER_STEP);
const int MASK = BASE - 1;
const int NO_BUCKETS = BASE;
int w[N_MAX], v[N_MAX];
int freq[NO_BUCKETS];
void RadixSort(int w[], int n, int bits) {//LSD
if(bits >= MAX_BITS)
return;
for(int i = 0; i < NO_BUCKETS; ++i)
freq[i] = 0;
for(int i = 0; i < n; ++i)
++freq[(w[i] >> bits) & MASK];
int start = 0, aux;
for(int i = 0; i < NO_BUCKETS; ++i){
aux = freq[i];
freq[i] = start;
start += aux;
}
for(int i = 0; i < n; ++i)
v[freq[(w[i] >> bits) & MASK]++] = w[i];
for(int i = 0; i < n; ++i)
w[i] = v[i];
RadixSort(w, n, bits + BITS_PER_STEP);
}
int main() {
int n;
fin >> n;
for(int i = 0; i < n; ++i)
fin >> w[i];
RadixSort(w, n, 0);
for(int i = 0; i < n; ++i)
fout << w[i] << ' ';
fout.put('\n');
return 0;
}