-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap-sort.js
68 lines (58 loc) · 1.92 KB
/
heap-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
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
const { GenericSort } = require('../../abstracts')
/**
* Heap Sort Algorithm.
* @augments GenericSort
* @memberof module:Algorithms
* @see page: 324.
* @see [edu.princeton.cs.algs4.Heap.java]{@link https://algs4.cs.princeton.edu/code/edu/princeton/cs/algs4/Heap.java.html}
*/
class Heap extends GenericSort {
/**
* Implementation of Heap Sort algorithm.
* @param {Array<*>} a - The array of values to be sorted.
* @param {ComparatorFn} [comparator] - The comparator function.
*/
static sort (a, comparator) {
let n = a.length
// constructs the max heap from the first half of the array
for (let k = Math.floor(n / 2); k >= 1; k--) {
this.sink(a, k, n, comparator)
}
// sort down
while (n > 1) {
// decrement indices by 1 so the array
// can be sorted from a[0] to a[n - 1]
this.exchange(a, 1 - 1, n - 1)
this.sink(a, 1, --n)
}
}
/**
* Sink algorithm. Restores the heap order by exchanging the node `k`
* down to the tree until is no longer smaller than any of its children.
* @param {Array<*>} a - The array of values treated also as the binary heap.
* @param {number} k - The index of the heap to sink.
* @param {number} n - The max length of the heap in which sink should run.
* @param {ComparatorFn} [comparator] - The comparator function.
*/
static sink (a, k, n, comparator) {
let j
while (2 * k <= n) {
j = 2 * k
// decrement indices by 1 so the array
// can be sorted from a[0] to a[n - 1]
if (j < n && this.less(a[j - 1], a[j + 1 - 1], comparator)) {
j++
}
// decrement indices by 1 so the array
// can be sorted from a[0] to a[n - 1]
if (!this.less(a[k - 1], a[j - 1], comparator)) {
break
}
// decrement indices by 1 so the array
// can be sorted from a[0] to a[n - 1]
this.exchange(a, k - 1, j - 1)
k = j
}
}
}
module.exports = Heap