-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell-sort.js
39 lines (34 loc) · 1019 Bytes
/
shell-sort.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
const { GenericSort } = require('../../abstracts')
/**
* Shell
* @augments GenericSort
* @classdesc Shell Sort Algorithm.
* @see p. 245, 259
* @see {@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Shell.java.html}
*/
class Shell extends GenericSort {
/**
* Sorts the `array` using the Shell Sort algorithm.
* @param {[*]} array Array of values.
* @param {function} comparator A comparing function that
* returns -1 when `a` is less than `b`,
* returns 1 when `a` is greater than `b` or
* returns 0 when `a` is equal to `b`.
*/
static sort (array, comparator) {
const n = array.length
let h = 1
while (h < Math.floor(n / 3)) {
h = 3 * h + 1 // 1, 4, 13, 40, 121, 364, 1093
}
while (h >= 1) {
for (let i = h; i < n; i++) {
for (let j = i; j >= h && this.less(array[j], array[j - h], comparator); j -= h) {
this.exchange(array, j, j - h)
}
}
h = Math.floor(h / 3)
}
}
}
module.exports = Shell