Pagini recente » Cod sursa (job #545451) | Cod sursa (job #1132296) | Cod sursa (job #1408696) | Cod sursa (job #126152) | Cod sursa (job #2611800)
#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;
}