-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueTest.cpp
More file actions
96 lines (78 loc) · 2.12 KB
/
QueueTest.cpp
File metadata and controls
96 lines (78 loc) · 2.12 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <queue>
static const int NUMBER_COUNT = 50;
static const int DIGIT_COUNT = 10;
void radixSort(std::queue<int> *queues, int *numbers);
int main()
{
// srand(time(0)); //Can be seeded for additional random output.
std::queue<int> radixQueues[DIGIT_COUNT];
int num[NUMBER_COUNT];
for (int i = 0; i < NUMBER_COUNT; ++i)
{
num[i] = (rand() % 1000);
if (num[i] < 889) num[i] += 111;
std::cout <<num[i];
if (i < NUMBER_COUNT) std::cout <<" ";
if (i % 10 == 9) std::cout <<"\n";
}
std::cout <<"\n";
radixSort(radixQueues, num);
for (int i = 0; i < NUMBER_COUNT; ++i)
{
std::cout <<num[i];
if (i < NUMBER_COUNT) std::cout <<" ";
if (i % 10 == 9) std::cout <<"\n";
}
return 0;
}
void radixSort(std::queue<int> *queues, int *numbers)
{
for (int i = 0; i < NUMBER_COUNT; ++i)
{
int rightSignificantDigit = 0;
rightSignificantDigit = numbers[i] % 10;
queues[rightSignificantDigit].push(numbers[i]);
}
int j = 0;
for (int i = 0; i < DIGIT_COUNT; ++i)
{
while (!queues[i].empty())
{
numbers[j++] = queues[i].front();
queues[i].pop();
}
}
for (int i = 0; i < NUMBER_COUNT; ++i)
{
int rightSignificantDigit = 0;
rightSignificantDigit = (numbers[i] % 100) / 10;
queues[rightSignificantDigit].push(numbers[i]);
}
j = 0;
for (int i = 0; i < DIGIT_COUNT; ++i)
{
while (!queues[i].empty())
{
numbers[j++] = queues[i].front();
queues[i].pop();
}
}
for (int i = 0; i < NUMBER_COUNT; ++i)
{
int rightSignificantDigit = 0;
rightSignificantDigit = (numbers[i] % 1000) / 100;
queues[rightSignificantDigit].push(numbers[i]);
}
j = 0;
for (int i = 0; i < DIGIT_COUNT; ++i)
{
while (!queues[i].empty())
{
numbers[j++] = queues[i].front();
queues[i].pop();
}
}
}