Cod sursa(job #1849489)

Utilizator alexkolumeRadulescu Ioan-Alexandru alexkolume Data 17 ianuarie 2017 16:47:36
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.98 kb
#include <fstream>
#include <queue>
using namespace std;

void radix(int v[], int n, int cifmax)
{
    queue<int> q[10];
    int p = 1;
    for (int i = 0; i <= cifmax; i++)
    {
        for (int j = 0; j < n; j++)
            q[(v[j] / p) % 10].push(v[j]);
        for (int j = 0, cif = 0; cif < 10; cif++)
        {
            while (!q[cif].empty())
            {
                v[j] = q[cif].front();
                q[cif].pop();
                j++;
            }
        }
        p *= 10;
    }
}

int main()
{
    ifstream fin("algsort.in");
    ofstream fout("algsort.out");
    int n, v[500000], cifmax = 0, putmax = 1;
    fin >> n;
    for (int i = 0; i < n; i++)
    {
        fin >> v[i];
        while (v[i] / 10 >= putmax)
        {
            cifmax++;
            putmax *= 10;
        }
    }
    radix(v, n, cifmax);
    for (int i = 0; i < n; i++)
        fout << v[i] << " ";
    fin.close();
    fout.close();
    return 0;
}