Pagini recente » Cod sursa (job #2930300) | Cod sursa (job #877602) | Cod sursa (job #87177) | Cod sursa (job #748990) | Cod sursa (job #1734113)
#include <iostream>
#include <fstream>
using namespace std;
const int nmax = 10000001;
int a[nmax];
int temp[nmax];
int counts[257];
int *ap, *tempp, *aux;
int n;
int A,B,C;
int i;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
void radixSort();
int main()
{
fin >> n;
for(int i=0; i<n; i++)
{
fin >> a[i];
}
ap = a;
tempp = temp;
radixSort();
for(int i=0; i<n; i++)
{
fout << ap[i] << " ";
}
fout << '\n';
fin.close();
fout.close();
return 0;
}
void radixSort()
{
for(int digit = 0; digit <4; digit++)
{
for( i=0;i<256; i++)
counts[i] =0;
for(i=0; i<n; i++)
counts[(ap[i]>>(digit*8))&255]++;
for(i=1; i<256; i++)
counts[i] +=counts[i-1];
for(i=n-1; i>=0; i--)
{
tempp[counts[(ap[i]>>(digit*8))&255]-1] = ap[i];
counts[(ap[i]>>(digit*8))&255]--;
}
aux = tempp;
tempp = ap;
ap = aux;
}
}