Pagini recente » Cod sursa (job #1550880) | Cod sursa (job #2740892) | Cod sursa (job #538087) | Cod sursa (job #2081153) | Cod sursa (job #1754580)
#include <fstream>
#include <algorithm>
#include <queue>
using namespace std;
int* ReadVector(int n, istream &fin);
void RadixSort(int n, int* list);
int main()
{
ifstream fin;
ofstream fout;
fout.open("algsort.out");
fin.open("algsort.in");
int n;
fin >> n;
int* list = ReadVector(n, fin);
RadixSort(n, list);
for(int i = 1; i <= n; i ++)
{
fout << list[i] << " ";
}
fin.close();
fout.close();
return 0;
}
int* ReadVector(int n, istream &fin)
{
int *list = new int[n + 1]();
for(int i = 1; i <= n; i++)
fin >> list[i];
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++;
}
}
}
}