-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSDStringSort.js
62 lines (42 loc) · 1.06 KB
/
LSDStringSort.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
class LSDStringSort {
sort(wordArray, wordSize) {
const arraySize = wordArray.length
const R = 256
const auxiliaryArray = []
for (let d = wordSize - 1; d >= 0; d--) {
const count = []
for (let i = 0; i < arraySize; i++) {
const word = wordArray[i]
if (!word) {
continue
}
const letter = word.charAt(d)
const countIndex = parseInt(letter, 16) + 1
if (!count[countIndex]) {
count[countIndex] = 0
}
count[countIndex]++
}
for (let r = 0; r < R; r++) {
count[r + 1] += count[r]
}
for (let i = 0; i < arraySize; i++) {
const word = wordArray[i]
if (!word) {
continue
}
const letter = word.charAt(d)
const countIndex = parseInt(letter, 16) + 1
if (!count[countIndex]) {
count[countIndex] = 0
}
auxiliaryArray[count[countIndex]] = wordArray[i]
count[countIndex]++
}
for (let i = 0; i < arraySize; i++) {
wordArray[i] = auxiliaryArray[i]
}
}
}
}
module.exports = LSDStringSort