Cod sursa(job #1752767)

Utilizator dtoniucDaniel Toniuc dtoniuc Data 5 septembrie 2016 09:34:36
Problema Radix Sort Scor 30
Compilator cpp Status done
Runda Arhiva educationala Marime 1.38 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 digitNumber, int* list);
int GetDigitNumber(int x);

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);
    int digitNumber = max(GetDigitNumber(b), GetDigitNumber(c));
    RadixSort(n, digitNumber, 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] = (a * list[i-1] + b) % c;
    }

    return list;
}

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

	int div = 1;

	for(int j = 1; j <= digitNumber; j++)
	{
		for(int i = 1; i <= n; i++)
		{
			int digit = (list[i] / div) % 10;
			queueList[digit].push(list[i]);
		}

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

				k++;
			}
		}

		div *= 10;
	}

}

int GetDigitNumber(int x)
{
	if(x == 0)
		return 1;

	int digit = 0;
	while(x != 0)
	{
		digit++;
		x /= 10;
	}

	return digit;
}