-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.cpp
More file actions
116 lines (92 loc) · 2.48 KB
/
radix_sort.cpp
File metadata and controls
116 lines (92 loc) · 2.48 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cmath>
#include <chrono>
using std::cout, std::endl;
void radix(int *data, int count)
{
int max = data[0];
for (int i = 1; i < count; i++)
{
if (data[i] > max)
max = data[i];
}
int powerOfTen{0};
while (max > 0)
{
max /= 10;
powerOfTen++;
}
int sorted[100] = {};
int divisor = 1;
for (int currPot{0}; currPot < powerOfTen; currPot++)
{
int i{0};
for (int currDig{0}; currDig <= 9; currDig++)
{
for (int n{0}; n < count; n++)
{
int digit = (data[n] / divisor) % 10;
if (digit == currDig)
{
sorted[i] = data[n];
i++;
}
}
}
for (int k{0}; k < count; ++k)
{
data[k] = sorted[k];
}
divisor *= 10;
}
}
int main()
{
int data[100] = {
834, 120, 932, 74, 543, 681, 217, 301, 64, 989,
110, 745, 502, 229, 361, 785, 156, 480, 27, 901,
329, 708, 36, 973, 812, 633, 509, 440, 678, 553,
91, 381, 205, 703, 643, 150, 226, 494, 603, 88,
915, 218, 577, 444, 142, 328, 830, 472, 12, 59,
431, 342, 249, 780, 678, 90, 11, 999, 64, 300,
438, 169, 350, 513, 236, 222, 600, 300, 512, 33,
134, 177, 466, 478, 821, 303, 2, 703, 155, 451,
739, 900, 84, 690, 411, 688, 207, 511, 315, 424,
689, 271, 65, 30, 750, 601, 521, 990, 77, 399};
int countData = sizeof(data) / sizeof(int);
cout << "Input: " << endl;
for (int d{0}; d < countData; d++)
{
cout << data[d];
if (d < countData - 1)
{
cout << ", ";
}
if (d > 1 &&
d % 25 == 0)
{
cout << endl;
}
}
auto start = std::chrono::high_resolution_clock::now();
radix(data, countData);
auto end = std::chrono::high_resolution_clock::now();
cout << endl << "Output: " << endl;
for (int d{0}; d < countData; d++)
{
cout << data[d];
if (d < countData - 1)
{
cout << ", ";
}
if (d > 1 &&
d % 25 == 0)
{
cout << endl;
}
}
std::chrono::duration<double, std::micro> duration = end - start;
std::cout << endl << "Execution time: " << duration.count() << " microseconds ("
<< duration.count() / 1000 << "ms)" << std::endl;
return 0;
}