-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSortCpp.cpp
More file actions
125 lines (104 loc) · 2.46 KB
/
RadixSortCpp.cpp
File metadata and controls
125 lines (104 loc) · 2.46 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
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <cmath>
using namespace std;
template <class T>
void Print(T& vec, int n, string s) {
cout << s << ": [" << flush;
for (int i = 0; i < n; i++) {
cout << vec[i] << flush;
if (i < n - 1) {
cout << ", " << flush;
}
}
cout << "]" << endl;
}
int Max(int A[], int n) {
int max = -32768;
for (int i = 0; i < n; i++) {
if (A[i] > max) {
max = A[i];
}
}
return max;
}
// Linked List node
class Node {
public:
int value;
Node* next;
};
int countDigits(int x) {
int count = 0;
while (x != 0) {
x = x / 10;
count++;
}
return count;
}
void initializeBins(Node** p, int n) {
for (int i = 0; i < n; i++) {
p[i] = nullptr;
}
}
void Insert(Node** ptrBins, int value, int idx) {
Node* temp = new Node;
temp->value = value;
temp->next = nullptr;
if (ptrBins[idx] == nullptr) {
ptrBins[idx] = temp; // ptrBins[idx] is head ptr
}
else {
Node* p = ptrBins[idx];
while (p->next != nullptr) {
p = p->next;
}
p->next = temp;
}
}
int Delete(Node** ptrBins, int idx) {
Node* p = ptrBins[idx]; // ptrBins[idx] is head ptr
ptrBins[idx] = ptrBins[idx]->next;
int x = p->value;
delete p;
return x;
}
int getBinIndex(int x, int idx) {
return (int)(x / pow(10, idx)) % 10;
}
void RadixSort(int A[], int n) {
int max = Max(A, n);
int nPass = countDigits(max);
// Create bins array
Node** bins = new Node * [10];
// Initialize bins array with nullptr
initializeBins(bins, 10);
// Update bins and A for nPass times
for (int pass = 0; pass < nPass; pass++) {
// Update bins based on A values
for (int i = 0; i < n; i++) {
int binIdx = getBinIndex(A[i], pass);
Insert(bins, A[i], binIdx);
}
// Update A with sorted elements from bin
int i = 0;
int j = 0;
while (i < 10) {
while (bins[i] != nullptr) {
A[j++] = Delete(bins, i);
}
i++;
}
// Initialize bins with nullptr again
initializeBins(bins, 10);
}
// Delete heap memory
delete[] bins;
}
int main() {
int A[] = { 237, 146, 259, 348, 152, 163, 235, 48, 36, 62 };
int n = sizeof(A) / sizeof(A[0]);
Print(A, n, "\t\tA");
RadixSort(A, n);
Print(A, n, " Sorted A");
return 0;
}