-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathheapsort.js
89 lines (80 loc) · 2.78 KB
/
heapsort.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* @module lib/heapsort
* @license MIT Copyright 2014 Daniel Imms (http://www.growingwiththeweb.com)
*/
'use strict';
var attachObserver = require('./common/attach-observer');
var defaultSwap = require('./common/default-swap');
var exposeCompareObserver = require('./common/expose-compare-observer');
var exposeSwapObserver = require('./common/expose-swap-observer');
var wrapCompare = require('./common/wrap-compare');
/**
* Heapify an array.
* @param {Array} array The array to build a heapify.
* @param {number} heapSize The size of the heap.
* @param {number} i The index of the array to heapify.
* @param {function} compare The compare function.
* @param {function} swap A function to call when the swap operation is
* performed. This can be used to listen in on internals of the algorithm.
* @returns The sorted array.
*/
function heapify(array, heapSize, i, compare, swap) {
var left = i * 2 + 1;
var right = i * 2 + 2;
var largest = i;
if (left < heapSize && compare(array, left, largest) > 0) {
largest = left;
}
if (right < heapSize && compare(array, right, largest) > 0) {
largest = right;
}
if (largest !== i) {
swap(array, i, largest);
heapify(array, heapSize, largest, compare, swap);
}
}
/**
* Build a heap out of an array.
* @param {Array} array The array to build a heap on.
* @param {number} heapSize The size of the heap.
* @param {function} compare The compare function.
* @param {function} swap A function to call when the swap operation is
* performed. This can be used to listen in on internals of the algorithm.
* @returns The sorted array.
*/
function buildHeap(array, heapSize, compare, swap) {
for (var i = Math.floor(array.length / 2); i >= 0; i--) {
heapify(array, heapSize, i, compare, swap);
}
}
/**
* Sorts an array using heapsort.
* @param {Array} array The array to sort.
* @param {function} compare The compare function.
* @param {function} swap A function to call when the swap operation is
* performed. This can be used to listen in on internals of the algorithm.
* @returns The sorted array.
*/
function sort(array, compare, swap) {
var heapSize = array.length;
buildHeap(array, heapSize, compare, swap);
while (heapSize > 1) {
swap(array, 0, --heapSize);
heapify(array, heapSize, 0, compare, swap);
}
return array;
}
/**
* Sorts an array.
* @param {Array} array The array to sort.
* @param {function} customCompare A custom compare function.
* @returns The sorted array.
*/
function sortExternal(array, customCompare) {
var compare = wrapCompare(customCompare, sortExternal.compareObserver);
var swap = attachObserver(defaultSwap, sortExternal.swapObserver);
return sort(array, compare, swap);
};
exposeCompareObserver(sortExternal);
exposeSwapObserver(sortExternal);
module.exports = sortExternal;