/* * queue-01.cc Standard containers, std::queue, exercise 1. * * Radix sort. */ #include #include #include #include #include using namespace std; template ostream& operator<<(ostream& os, const vector& v) { unsigned n{ 0 }; for (const auto& x : v) { os << setw(4) << x << ((++n % 20 == 0) ? '\n' : '\0'); } return os; } template void radixsort(vector& v, int n_digits, unsigned radix = 10) { vector> q{ radix }; for (int d = 0; d < n_digits; ++d) { // distribute int divisor = static_cast(pow(static_cast(radix), d)); for (const auto& x : v) { q[(x / divisor) % radix].push(x); } // reassemble auto v_pos = v.begin(); for (int i = 0; i < radix; ++i) { while (! q[i].empty()) { *v_pos++ = q[i].front(); q[i].pop(); } } } } int main() { vector v; // random number engine, the source of random numbers std::mt19937 rng; // distribution std::uniform_int_distribution uint_dist100{ 0, 100 }; for (int i = 0; i < 200; ++i) { // the engine is passed to the function call operator of the // distribution to generate the next value. v.push_back(uint_dist100(rng)); } cout << "Osorterade värden:\n" << v << endl; radixsort(v, 3, 10); cout << "Sorterade värden:\n" << v << endl; return 0; }