|
5 | 5 | return a - b;
|
6 | 6 | }
|
7 | 7 |
|
8 |
| - /** |
9 |
| - * The heapsort algorithm. It's complexity is O(nlog n). |
10 |
| - * |
11 |
| - * @public |
12 |
| - */ |
13 | 8 | var heapSort = (function () {
|
14 | 9 |
|
15 | 10 | /**
|
16 | 11 | * Finds the correct place of given element in given max heap.
|
17 | 12 | *
|
18 | 13 | * @private
|
19 |
| - * @param {array} array Array |
20 |
| - * @param {number} index Index of the element which palce in |
21 |
| - * the max heap should be found. |
| 14 | + * @param {Array} array Array. |
| 15 | + * @param {Number} index Index of the element which palce in |
| 16 | + * the max heap should be found. |
22 | 17 | */
|
23 | 18 | function heapify(array, index, heapSize, cmp) {
|
24 | 19 | var left = 2 * index + 1;
|
|
45 | 40 | * Builds max heap from given array.
|
46 | 41 | *
|
47 | 42 | * @private
|
48 |
| - * @param {array} array Array which should be turned into max heap |
49 |
| - * @returns {array} array Array turned into max heap |
| 43 | + * @param {Array} array Array which should be turned into max heap. |
| 44 | + * @return {Array} array Array turned into max heap. |
50 | 45 | */
|
51 | 46 | function buildMaxHeap(array, cmp) {
|
52 | 47 | for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) {
|
|
56 | 51 | }
|
57 | 52 |
|
58 | 53 | /**
|
59 |
| - * Heapsort. Turns the input array into max heap and after that sorts it. |
| 54 | + * Heapsort. Turns the input array into max |
| 55 | + * heap and after that sorts it.<br><br> |
| 56 | + * Time complexity: O(N log N). |
| 57 | + * |
| 58 | + * @example |
| 59 | + * |
| 60 | + * var sort = require('path-to-algorithms/src' + |
| 61 | + * '/sorting/heapsort').heapSort; |
| 62 | + * console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ] |
60 | 63 | *
|
61 | 64 | * @public
|
62 |
| - * @param {array} array Input array |
63 |
| - * @returns {array} array Sorted array |
| 65 | + * @module sorting/heapsort |
| 66 | + * @param {Array} array Input array. |
| 67 | + * @return {Array} Sorted array. |
64 | 68 | */
|
65 | 69 | return function (array, cmp) {
|
66 | 70 | cmp = cmp || comparator;
|
|
80 | 84 |
|
81 | 85 | exports.heapSort = heapSort;
|
82 | 86 |
|
83 |
| -}(typeof exports === 'undefined' ? window : exports)); |
| 87 | +})(typeof window === 'undefined' ? module.exports : window); |
0 commit comments