-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
273 lines (273 loc) · 9.59 KB
/
Copy pathindex.html
File metadata and controls
273 lines (273 loc) · 9.59 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Radix Sort LSD page 1</title>
<link rel ="stylesheet" type = "text/css" href = "./RadixSort/css/stylesheet.css">
</head>
<body>
<div class = "container">
<header class="top-section">
<div class = "bigtxt">Creating an LSD Radix Sort with <span style="color:#69c623">JavaScript</span>
</div>
</header>
<div class = "default">
<p class = "medtxt">Section A1 <i>- <span font style="font-size:18px;">The Queue Class Data Structure and LSD Radix Function</span></i></p>
<p>In this example I will demonstrate creating an LSD(least significant digit) radix sort in JavaScript
using the queue class(FIFO) as our data structure. The LSD radix sort belongs to the family of
distribution sorts. It is extremely efficient for integer sorting, especially with large values of n.
I've tested the sort on a MacBook Air (Intel i5@1.4Ghz, 4GB 1600 MHz DDR3, 128GB SSD) with n = 900,000
random integers of values ranging from 0 - 99. The runtime results were on average 500 ms. That's quite
fast for a somewhat mediocre system , though a more accurate measure of efficiency would be measuring
the number of operations involved. Measuring efficiency based on the number of operations is more accurate
due to the fact that all computer systems are not created equal, so run times may vary based on the how
powerful the hardware components are and amount of memory the system has. You can research on-line "Big O"
notation and find out more about how to determine efficiency of an algorithm based on the number of operations.
</p>
<p>The LSD radix sort starts by enqueuing integers based on least significant digit of the integer, then works
it's way inwards towards the most significant digit. The process involves a series of sweeps and the number
of sweeps is dependent upon the largest value of the integer contained within the array of integers which
is to be sorted. In this case, for simplicity we will keep the values limited to integers ranging from 0
to 99. This will require 2 enqueue sweeps; the first sweep based on the 1's digit and the second sweep based on
the 10's digit. Also the function will sort the data in place, so there will be no need to create a copy of the
input data array. This will result in more efficiency as the input parameter array becomes significantly larger,
however if for some reason you would want to preserve the original input data array then working with a copy of
it would be a better choice
</p>
<p>The data structure we will use will be based on the Queue class and will require three functions and an empty array, dataStore.
The Queue class is defined in lines 1 - 23 below.
</p>
<div class = "block1">
<div class = "code">
<pre>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
</pre>
</div>
<div class = "code">
<pre>
function Queue(){
this.dataStore = [];
this.enqueue = enqueue;
this.dequeue = dequeue;
this.isEmpty = isEmpty;
};
function enqueue(<i>element</i>){
this.dataStore.push(element);
};
function dequeue(){
if(this.dataStore.length == 0){
return false;
} else {
return this.dataStore.shift();
}
};
function isEmpty(){
if(this.dataStore.length == 0){
return true;
} else {
return false;
}
};
</pre>
</div>
</div>
<p>The next step will constructing the radix function. This function will take in one parameter(data), which is the
unsorted array of integers of values ranging from 0 - 99 and initialize two following empty arrays:
</p>
<ul>
<li>bin: This array will be used to contain 10 instances of the Queue class</li>
<li>digIndex: This array will be used for mapping the integers stored in the queues of the bin array to the
index position
in data array.</li>
</ul>
<p>This radix function primarily consists of two blocks of code which is commented in the code below. For this example
I will step through the code line by line so we can have a clear understanding of the process of how the unsorted
integers are mapped and enqueued into their appropriate queues in the bin array and then re-mapped and dequeued
into their proper index position of the data array. The data array we pass in as a parameter will be overwritten
and sorted after the radix sort. The radix function I will use is listed below. It's also important to note that
there are other ways of coding the radix function but the concept and procedures remain relatively the same among different
variations.
</p>
<div class = "block1">
<div class = "code">
<pre>
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
</pre>
</div>
<div class = "code">
<pre>
function radix(<i>data</i>){
<i>var</i> bin = [];
<i>var</i> digIndex = [];
for(<i>var</i> i = 0; i < 10; i++){
bin[i] = new Queue();
}; // Block 1------------------------------
for(<i>var</i> i = 0; i < data.length; i++){
bin[data[i]%10].enqueue(data[i]);
};
for(<i>var</i> i = 0; i < bin.length; i++){
digIndex.push(bin[i].dataStore.length);
};
for(<i>var</i> i = 0; i < digIndex.length - 1; i++){
digIndex[i + 1] += digIndex[i];
};
for(<i>var</i> i = bin.length - 1; i >= 0; i--){
while(!bin[i].isEmpty()){
data[--digIndex[i]] = bin[i].dequeue();
}
}; // Block 2------------------------------
digIndex = []; // re-initialize digIndex
for(<i>var</i> i = data.length - 1; i >= 0; i--){
bin[Math.floor(data[i]/10)%10].enqueue(data[i]);
};
for(<i>var</i> i = 0; i < bin.length; i++){
digIndex.push(bin[i].dataStore.length);
};
for(<i>var</i> i = 0; i < digIndex.length - 1; i++){
digIndex[i + 1] += digIndex[i];
};
for(<i>var</i> i = bin.length - 1; i >= 0; i--){
while(!bin[i].isEmpty()){
data[--digIndex[i]] = bin[i].dequeue();
}
};
return data;
};
</pre>
</div>
</div>
<p>It is necessary that we have 10 empty instances of the Queue class stored in the bin array, which
will be explained in more detail later in this article. Lines 27 - 29 of the radix function we use a for
loop to create and store 10 instances of the Queue class within the bin array as shown below.
</p>
<div class = "block1">
<div class = "code">
<pre>
27
28
29
</pre>
</div>
<div class = "code">
<pre>
for(<i>var</i> i = 0; i < 10; i++){
bin[i] = new Queue();
};
</pre>
</div>
</div>
<p>The result will be the array (bin) of ten objects, bin[0] to bin[9] which are the instances of the
Queue class as shown below. Take notice of, dataStore: [ ], which indicates all queues contained
in the bin array are empty. If you are using an editor such as Sublime Text, you could insert console.log(bin)
after line 32 of the radix function to view the bin array as shown below. You will find it very useful to often
use console.log for debugging purposes in cases when your code crashed or did not function as intended. This
minimizes the time it takes to pinpoint where the code went wrong while at the same time provides a visual
snapshot of current variable values.
</p>
<div class = "code">
<pre>
[ { dataStore: [], // bin[0]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[1]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[2]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[3]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[4]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[5]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[6]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[7]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[8]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] },
{ dataStore: [], // bin[9]
enqueue: [Function: enqueue],
dequeue: [Function: dequeue],
isEmpty: [Function: isEmpty] } ]
</pre>
</div>
<center><div class = "botNav"><a href = "./RadixSort/radix_pg2.html"></a></div></center>
</div>
</div>
</body>
</html>