Cod sursa(job #1752773)

Utilizator dtoniucDaniel Toniuc dtoniuc Data 5 septembrie 2016 10:01:33
Problema Radix Sort Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <fstream>
#include <algorithm>
#include <queue>

using namespace std;

int* CreateVector(int n, int a, int b, int c);
void RadixSort(int n, int* list);

int main()
{
    ifstream fin;
    ofstream fout;
    fout.open("radixsort.out");
    fin.open("radixsort.in");

    int n, a, b, c;
    fin >> n >> a >> b >> c;

    int* list = CreateVector(n, a, b, c);
    RadixSort(n, list);

    for(int i = 1; i <= n; i += 10)
    {
    	fout << list[i] << " ";
    }

    fin.close();
    fout.close();
    return 0;
}

int* CreateVector(int n, int a, int b, int c)
{
    int *list = new int[n + 1]();

    list[1] = b;

    for(int i = 2; i <= n; i++)
    {
    	list[i] = ((1LL * a * list[i-1]) % c + b) % c;
    }

    return list;
}

void RadixSort(int n, int* list)
{
	queue<int> queueList[257];

	for(int j = 0; j < 4; j++)
	{
		for(int i = 1; i <= n; i++)
		{
			int byte = (list[i] >> (j * 8)) & 0xFF;

			queueList[byte].push(list[i]);
		}

		int k = 1;
		for(int i = 0; i < 257; i++)
		{
			while(!queueList[i].empty())
			{
				list[k] = queueList[i].front();
				queueList[i].pop();

				k++;
			}
		}
	}
}