Pagini recente » Cod sursa (job #2976529) | Cod sursa (job #3184438) | Cod sursa (job #1869754) | Cod sursa (job #1427303) | Cod sursa (job #2479689)
#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");
int 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<int> b[])
{
cout << "The bucket is: \n";
for(int i=0; i<10;i++)
{
cout << i << ": ";
for(auto nr:b[i])
cout << nr << ' ';
cout << '\n';
}
}
void placeInBuckets(int ex)
{
vector<int> bucket[10];
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;
}
}
}
void radixSort()
{
///get max
int exmax = v[0];
for(int i=1; i<n; i++)
exmax = max(exmax,v[i]);
for(int 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;
}