|
1 |
| -import nextchild from './nextchild.js' ; |
| 1 | +import nextchild from './nextchild.js'; |
2 | 2 |
|
3 | 3 | /**
|
4 | 4 | * Sifts down a node.
|
5 | 5 | *
|
6 | 6 | * @param {function} compare the comparison function
|
7 | 7 | * @param {array} a the array where the heap is stored
|
8 |
| - * @param {int} i is the root element |
9 |
| - * @param {int} j - 1 is the last leaf |
10 |
| - * @param {int} k is the target node |
| 8 | + * @param {number} i is the root element |
| 9 | + * @param {number} j - 1 is the last leaf |
| 10 | + * @param {number} k is the target node |
11 | 11 | */
|
12 | 12 |
|
13 |
| -export default function siftdown ( compare, a, i, j, k ) { |
14 |
| - |
| 13 | +export default function siftdown(compare, a, i, j, k) { |
15 | 14 | let current = k - i;
|
16 | 15 |
|
17 |
| - while ( true ) { |
18 |
| - |
19 |
| - // address of the first child in a zero-based |
| 16 | + while (true) { |
| 17 | + // Address of the first child in a zero-based |
20 | 18 | // binary heap
|
21 | 19 |
|
22 | 20 | const firstchild = 2 * current + 1;
|
23 | 21 |
|
24 |
| - // if current node has no children |
| 22 | + // If current node has no children |
25 | 23 | // then we are done
|
26 | 24 |
|
27 |
| - if ( firstchild >= j - i ) break ; |
| 25 | + if (firstchild >= j - i) break; |
28 | 26 |
|
29 |
| - // if current value is smaller than its smallest |
| 27 | + // If current value is smaller than its smallest |
30 | 28 | // child then we are done
|
31 | 29 |
|
32 |
| - const candidate = nextchild( compare, a, i + firstchild, j ); |
| 30 | + const candidate = nextchild(compare, a, i + firstchild, j); |
33 | 31 |
|
34 |
| - if ( compare( a[i + current], a[candidate] ) <= 0 ) break ; |
| 32 | + if (compare(a[i + current], a[candidate]) <= 0) break; |
35 | 33 |
|
36 |
| - // otherwise |
| 34 | + // Otherwise |
37 | 35 | // swap with smallest child
|
38 | 36 |
|
39 |
| - const tmp = a[i+current] ; |
40 |
| - a[i+current] = a[candidate] ; |
41 |
| - a[candidate] = tmp ; |
| 37 | + const tmp = a[i + current]; |
| 38 | + a[i + current] = a[candidate]; |
| 39 | + a[candidate] = tmp; |
42 | 40 |
|
43 | 41 | current = candidate - i;
|
44 |
| - |
45 | 42 | }
|
46 | 43 |
|
47 | 44 | return i + current;
|
48 |
| - |
49 | 45 | }
|
0 commit comments