-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathRadixSort.js
89 lines (77 loc) · 2.16 KB
/
RadixSort.js
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
/* Description :
The idea of Radix Sort is to do digit by digit sort starting from least significant
digit to most significant digit. Radix sort uses counting sort as a subroutine to sort.
*/
/* JAVA SCRIPT CODE */
(function (exports) {
'use strict';
var radixSort = (function () {
var getDigit = function (number, lsdOffset) {
var size = number.toString().length;
var digit;
if (lsdOffset >= 0 && lsdOffset < size) {
digit = number.toString()[size - 1 - lsdOffset];
}
return digit;
};
return function (array) {
var size = array.length;
var R = 10; /* Alphabet size ([0-9] for integers) */
var count;
var digit;
var i;
var j;
/* Find maximum key size */
var maxKeySize = (array[0] || '').toString().length;
for (i = 1; i < size; i += 1) {
var numStr = array[i].toString();
if (numStr.length > maxKeySize) {
maxKeySize = numStr.length;
}
}
for (i = 0; i < maxKeySize; i += 1) {
/* Initialize count */
count = [];
for (j = 0; j < R; j += 1) {
count[j] = 0;
}
/* Count frequency of each array element */
for (j = 0; j < size; j += 1) {
digit = getDigit(array[j], i) || 0;
count[digit] += 1;
}
/* Compute cumulates */
for (j = 1; j < R; j += 1) {
count[j] += count[j - 1];
}
/* Move elements to auxiliary array */
var aux = [];
for (j = size - 1; j >= 0; j -= 1) {
digit = getDigit(array[j], i) || 0;
count[digit] -= 1;
aux[count[digit]] = array[j];
}
/* Copy elements back from auxilary array */
for (j = 0; j < size; j += 1) {
array[j] = aux[j];
}
}
return array;
};
})();
exports.radixSort = radixSort;
})(typeof window === 'undefined' ? module.exports : window);
/*
Test Case 1:
Unsorted list:
170, 45, 75, 90, 802, 24, 2, 66
Output:
2, 24, 45, 66, 75, 90, 170, 802
Test Case 2:
Unsorted list:
573, 25, 415, 12, 161, 6
Ouput:
6 12 25 161 415 573
Time Complexity : O(n)
Space Complexity : O(n)
*/