Pagini recente » Cod sursa (job #3281325) | Cod sursa (job #2721664)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
const int INF = (1ll << 31) - 1;
void radixSort(vector<int> &arr, int mx = INF, int basePow = 8)
{
int base = 1 << 8;
int mask = base - 1;
vector<int> newarr(arr.size()), cnt(base), start(base);
long long fact = 1;
int sh = 0;
while(mx / fact > 0)
{
// compute start position
cnt.assign(base, 0);
for(int x : arr)
cnt[(x >> sh) & mask]++;
start[0] = 0;
for(int i = 1; i < base; i++)
start[i] = start[i-1] + cnt[i-1];
for(int x : arr)
{
int coef = (x >> sh) & mask;
newarr[start[coef]++] = x;
}
arr = newarr;
fact *= base;
sh += basePow;
}
}
int main()
{
int n;
fin >> n;
vector<int> v(n);
int mx = 0;
for(int i = 0; i < n; i++)
{
fin >> v[i];
if(v[i] > mx)
mx = v[i];
}
radixSort(v, mx);
for(int i = 0; i < n; i++)
fout << v[i] << ' ';
return 0;
}