-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradix-sort.cc
220 lines (174 loc) · 5.03 KB
/
radix-sort.cc
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// C++ LSD Radix Sort example, queue implementation
// TBD: use queue STL
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef struct slist_ {
int val;
struct slist_ *next;
} slist;
void Print (slist *start)
{
slist *temp = start;
while (temp->next != NULL)
{
cout << temp->val << " ";
temp = temp->next;
}
cout << endl;
}
/*
update:
void radiosort (int array[], int maxDigitalNumber)
build a map<int key, vector<int> >;
m = 1;
for each digital number j: [1,t]
for all array[i]
retreive j-th digital number: static_cast<int>(array[i]/m)%10
map array[i] to (j-th digital number, values list) // all values with same j-th digital number
copy all values in map into array[];
m = m * 10; // retreive next digital number
*/
slist *radixsort(slist *L, int t)
{
int i, j, d, m=1;
slist *temp, *head[10], *tail[10];
// Process t digits
for (j=1; j<=t; j++)
{
// Initialize the queues, 0 to 9
for (i=0; i<=9; i++)
{
head[i] = NULL;
tail[i] = NULL;
}
/*sort each digital numbers*/
// Process the list L
// e.g., head[d] --> node 1 --> node 2 <-- tail[d], d is the m-th digital number
while ( L != NULL )
{
// Get the decimal digit with place value m
d = static_cast<int>(L->val/m)%10;
// Make temp point to the current list item
temp = L;
// Make L point to the next list item
L = L->next;
// Disconnect the current list item, making it self-contained
temp->next = NULL;
/*for all the numbers with same value on m-th digintal number, enque
e.g., head[6] --> 426 --> 146 --> tail[6]
*/
if (head[d]!= NULL)
{ // The queue for digit d is not empty
// Add the list item to the end of the queue for digit d
tail[d]->next = temp;
// Make tail[d] point to the new tail item of queue d
tail[d] = temp;
}
else
{ // The queue for digit d is empty
head[d] = temp; // Point to the new head
tail[d] = temp; // Point to the new tail
}
} // while
/* put all the numbers in the queues into original array L
* as such, the sequence of nodes in the same queue can be maintained
*/
// Find the index, d, of the first non-empty queue
d=0;
while (head[d]==NULL)
d++;
// Point to the first item of the first non-empty queue
L = head[d];
// Point to the last item of the first non-empty queue
temp = tail[d];
// Append the items of the remaining non-empty queues
// to the tail of list L.
d++;
while (d<10)
{
if (head[d]!=NULL)
{
// Append the items of non-empty queue d to list L
temp->next = head[d];
// Point to the new tail of list L
temp = tail[d];
}
d++;
} // while
// Prepare to process the next more significant digit
m = m*10;
//Print (L);
} // for
return L;
}
int main()
{
// Initialize the random number seed with the time
srand( static_cast<unsigned int>(time(NULL)) );
const int size = 10;
int n[size];
int i, max=-1, t=0;
// Generate some random numbers
slist *start=NULL,*end=NULL,*temp;
/*
for (i=0; i<size; i++)
{
n[i] = rand();
}
*/
n[0] = 624;
n[1] = 852;
n[2] = 426;
n[3] = 987;
n[4] = 269;
n[5] = 146;
n[6] = 415;
n[7] = 301;
n[8] = 730;
n[9] = 78;
// Find the largest value and create a linked list of the random values
for (i=0; i<size; i++)
{
// Find the largest value
if (n[i] > max)
max = n[i];
// Create a new node
temp = new slist;
// Fill the node with a random value
temp->val = n[i];
// Seal the node
temp->next = NULL;
if (start != NULL)
{ // Append the new node to the linked list
end->next = temp;
end = temp;
}
else
{ // Add the first node to the linked list
start = temp;
end = temp;
}
}
// Find the number of decimal digits in the largest random value
while (max>0)
{
t=t+1;
max=max/10;
}
//Print (start);
// Perform the radix sort
start = radixsort(start, t);
// Display the results
cout << "after radix sort.\n";
temp = start;
for (i=0; i<size; i++)
{
cout << temp->val << "\n";
temp = temp->next;
}
// Return a zero to the calling script, batch file, or command shell
// to indicate successful completion.
return 0;
} // main