Fork me on GitHub

Algorithms

This is a demonstration of selected algorithms from the book Introduction to Algorithms by Cormen et. al., implemented in JavaScript by Felix H. Dahlke.

Heapsort

A well performing sort algorithm that uses a binary heap.

Sorry, your browser doesn't support HTML5 Canvas.
var heapsort = (function() {
    function exchange(array, i, j) {
        var temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    function maxHeapify(array, root) {
        var left = (root === 0) ? 1 : 2 * root,
            right = left + 1,
            largest = (left < array.heapSize && array[left] > array[root])
                ? left : root;

        if (right < array.heapSize && array[right] > array[largest])
            largest = right;

        if (largest !== root) {
            exchange(array, root, largest);
            maxHeapify(array, largest);
        }
    }

    function buildMaxHeap(array) {
        array.heapSize = array.length;
        for (var i = Math.round(array.length / 2); i >= 0; i--) {
            maxHeapify(array, i);
            update(array);
        }
    }

    return function(array) {
        buildMaxHeap(array);
        for (var i = array.length - 1; i >= 1; i--) {
            exchange(array, 0, i);
            array.heapSize--;
            maxHeapify(array, 0);
            update(array);
        }
    };
})();