Cod sursa(job #2479683)

Utilizator vladbatalanBatalan Vlad vladbatalan Data 24 octombrie 2019 11:11:18
Problema Sortare prin comparare Scor 80
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.44 kb
#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';
    }
}

void placeInBuckets(ull ex)
{
    vector<ull> 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
    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;
}