Pagini recente » Cod sursa (job #2129077) | Profil Hermione | Cod sursa (job #1434467) | Cod sursa (job #2022028) | Cod sursa (job #1333487)
#include <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
const int digits = 2; //Digits
const int r = 16; //Bits
const int radix = 1 << r; //Buckets
const int mask = radix - 1; //BitMask
void RadixSort(vector<int>& a)
{
const int SIZE = a.size();
vector <int> b(SIZE);
vector <int> bucket(radix);
for (int i = 0, shift = 0; i < digits; ++i, shift += r)
{
for (int j = 0; j < radix; ++j)
bucket[j] = 0;
for (int j = 0; j < SIZE; ++j)
++bucket[(a[j] >> shift) & mask];
for (int j = 1; j < radix; j++)
bucket[j] += bucket[j - 1];
for (int j = SIZE - 1; j >= 0; --j)
b[--bucket[(a[j] >> shift) & mask]] = a[j];
for (int j = 0; j < SIZE; j++)
a[j] = b[j];
}
}
void read(vector<int>& a)
{
ifstream f("radixsort.in");
int N, A, B, C;
f >> N >> A >> B >> C;
a.push_back(B);
for (int i = 1; i < N; ++i)
{
int x = (1LL * A * a[i - 1] + B) % C;
a.push_back(x);
}
f.close();
}
void write(vector<int> a)
{
ofstream g("radixsort.out");
for (unsigned i = 0; i < a.size(); i += 10)
g << a[i] << " ";
g << "\n";
g.close();
}
int main()
{
vector<int> v;
read(v);
RadixSort(v);
write(v);
return 0;
}