Pagini recente » Cod sursa (job #1323485) | Cod sursa (job #2034710) | Cod sursa (job #1460461) | Istoria paginii runda/zombie | Cod sursa (job #2479686)
#include <iostream>
#include <fstream>
#include <vector>
#define ull unsigned long long
#define NMAX 500010
using namespace std;
//ofstream test("algsort.in");
ifstream fin("algsort.in");
ofstream fout("algsort.out");
ull v[NMAX];
int n;
/*void make_test(int n)
{
test << n << '\n';
for(int i=n; i>0; i--)
{
test << i;
if(i!=1)
test << ' ';
else
test << '\n';
}
}*/
void showBucket(vector<ull> b[])
{
cout << "The bucket is: \n";
for(int i=0; i<10;i++)
{
cout << i << ": ";
for(auto nr:b[i])
cout << nr << ' ';
cout << '\n';
}
}
vector<ull> bucket[10];
void placeInBuckets(ull ex)
{
for(int i=0; i<n; i++)
bucket[(v[i]/ex)%10].push_back(v[i]);
///out of bucket
int poz = 0;
for(int i=0; i<10; i++)
{
for(auto nr:bucket[i])
v[poz++] = nr;
bucket[i].clear();
}
}
void radixSort()
{
///get max
ull exmax = v[0];
for(int i=1; i<n; i++)
exmax = max(exmax,v[i]);
for(ull ex = 1; ex <= exmax*10; ex *=10)
placeInBuckets(ex);
}
void showVector()
{
for (int i=0; i < n; i++)
fout << v[i] << ' ';
fout << '\n';
}
int main()
{
//make_test(100);
fin >> n;
for(int i=0; i<n; i++)
fin >> v[i];
radixSort();
showVector();
return 0;
}