-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThreeWayStringQuickSort.js
46 lines (34 loc) · 1.04 KB
/
ThreeWayStringQuickSort.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
const Sort = require("../../sorting/Sort")
const Char = require("../Char")
class ThreeWayStringQuickSort extends Char {
#sortHelper = new Sort()
sort(array = []) {
const arraySize = array.length
this.#sortBySubArrays(array, 0, arraySize - 1, 0)
}
#sortBySubArrays(array, lowerIndex, higherIndex, d) {
if (higherIndex <= lowerIndex) {
return
}
let lt = lowerIndex
let gt = higherIndex
let index = lowerIndex + 1
const value = this.charAt(array[lowerIndex], d)
while (index <= gt) {
const comparableValue = this.charAt(array[index], d)
if (comparableValue < value) {
this.#sortHelper.exchangeValues(array, lt++, index++)
} else if (comparableValue > value) {
this.#sortHelper.exchangeValues(array, index, gt--)
} else {
index++
}
}
this.#sortBySubArrays(array, lowerIndex, lt - 1, d)
if (value >= 0) {
this.#sortBySubArrays(array, lt, gt, d + 1)
}
this.#sortBySubArrays(array, gt + 1, higherIndex, d)
}
}
module.exports = ThreeWayStringQuickSort