Cod sursa(job #2611800)

Utilizator felixiPuscasu Felix felixi Data 7 mai 2020 16:41:34
Problema Sortare prin comparare Scor 40
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.46 kb
#include <bits/stdc++.h>

using namespace std;

void RadixSort(std::vector<int>& v)
{
    // using Felix::Colors::File::red;
    // using Felix::Colors::File::reset;

    const int BASE = 10;
    const int valMax = *std::max_element(v.begin(), v.end());
    const int valMin = *std::min_element(v.begin(), v.end());
    // if (valMin < 0) {
    //     return red + "Negative values don't work with Radix Sort" + reset;
    // }
    std::queue<int> q[2][BASE];

    for( auto & x : v )
        q[0][x % BASE].push(x);
    int qind = 0, put = BASE;

    while(valMax >= put) {
        // cout << "hei\n";
        qind ^= 1;

        for( int i = 0;  i < BASE;  ++i ) {
            while( !q[1 ^ qind][i].empty() ) {
                int x = q[1 ^ qind][i].front();
                 std::cerr << x / put % BASE << ' ';
                q[1 ^ qind][i].pop();
                q[qind][x / put % BASE].push(x);
            }
        }
        /// std::cerr << '\n';
        if( valMax / put < BASE )
            break;
        put *= BASE;    
    }
    v.clear();
    for( int i = 0;  i < BASE;  ++i ) 
        while( !q[qind][i].empty() )
            v.push_back(q[qind][i].front()),
            q[qind][i].pop();
}

int main()
{
    ifstream in("algsort.in");
    ofstream out("algsort.out");
    int n;
    in >> n;
    vector<int> v(n);
    for (auto& i : v) in >> i;
    RadixSort(v);
    for (auto i : v) out << i << ' ';
    return 0;
}