Pagini recente » Cod sursa (job #765869) | Cod sursa (job #3217557) | Cod sursa (job #1619777) | Cod sursa (job #2727235) | Cod sursa (job #2534382)
#include <vector>
#include <cstdint>
#include <cstring>
#include <fstream>
#define TOTAL_BYTES 4U
#define RADIX_SIZE 8U
#define RADIX 0xff
#define GET_BYTE(num, byte) ((num >> (byte * RADIX_SIZE)) & RADIX)
using Point = std::pair<std::int32_t, std::int32_t>;
class Utilities
{
public:
static void CountSortByByte(const std::vector<std::uint32_t>& from, std::vector<std::uint32_t>& to, std::uint32_t byte)
{
std::uint32_t count[1 << RADIX_SIZE];
std::uint32_t index[1 << RADIX_SIZE];
memset(count, 0, sizeof(std::uint32_t) * (1 << RADIX_SIZE));
for (const std::uint32_t value : from)
{
count[GET_BYTE(value, byte)]++;
}
index[0] = 0;
for (std::uint32_t it = 1U; it < (1 << RADIX_SIZE); ++it)
{
index[it] = index[it - 1] + count[it - 1];
}
for (const std::uint32_t value : from)
{
to[index[GET_BYTE(value, byte)]++] = value;
}
}
static void RadixSort(std::vector<std::uint32_t>& data)
{
std::vector<std::uint32_t> temp(data.size(), 0);
for (std::uint32_t byte = 0U; byte < TOTAL_BYTES; ++byte)
{
if (byte & 1)
{
Utilities::CountSortByByte(temp, data, byte);
}
else
{
Utilities::CountSortByByte(data, temp, byte);
}
}
}
};
int main()
{
int n, a, b, c;
std::ifstream in("radixsort.in");
if (in.is_open())
{
in >> n >> a >> b >> c;
in.close();
}
std::vector<std::uint32_t> v(n, 0);
v[0] = b;
for (int i = 1; i < n; i++)
v[i] = (a * 1LL * v[i - 1] + b) % c;
Utilities::RadixSort(v);
std::ofstream out("radixsort.out");
if (out.is_open())
{
for (int i = 0; i < n; i += 10)
{
out << v[i] << " ";
}
out.close();
}
}