1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
#include <string>
#include <iostream>
#include "Queue.h"
using namespace std;
void drawLine()
{
cout << endl << string(50, '-') << endl << endl;
}
void radixSort(int *a, int n)
{
const int radix = 10;
int digits = 10;
Queue<int> queues[radix];
for(int i = 0, factor = 1; i < digits; factor *= radix, i++)
{
for(int j = 0; j < n; j++)
queues[((a[j]/factor)%radix)].push_back(a[j]);
int k = 0;
for(int j = 0; j < radix; j++)
{
while(!queues[j].empty())
{
a[k] = queues[j].front();
queues[j].pop_front();
k++;
}
}
}
}
void testRadixSort()
{
drawLine();
cout << "Test RadixSort()" << endl;
int a[] = {5, 8, 0, 2, 4, 1, 7, 9, 3, 6};
cout << "Original Array: ";
for(int i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl << endl;
radixSort(a, 10);
cout << "RadixSort: ";
for(int i = 0; i < 10; i++)
cout << a[i] << " ";
cout << endl;
}
int main()
{
testRadixSort();
return 0;
}
|