data-structures-and-algorithms/heapq

View on GitHub
src/heapify.js

Summary

Maintainability
A
0 mins
Test Coverage
import {siftdown, Heap} from './core/index.js';

/**
 * Builds a heap in O(n) operations.
 *
 * @param {function} compare the comparison function
 * @param {array} x the array where the heap is stored
 *
 */

export default function heapify(compare, x) {
    const n = x.length;

    // eslint-disable-next-line no-bitwise
    for (let k = (n / 2) | 0; k; ) {
        siftdown(compare, x, 0, n, --k);
    }

    return new Heap(compare, x);
}