|
1 | | -/** |
2 | | - * Finds the maximum subarray using the divide and conquer algorithm |
3 | | - * by Bentley, Jon (1984) (complexity O(n(logn))); |
4 | | - */ |
5 | | - |
6 | 1 | (function (exports) { |
7 | 2 | 'use strict'; |
8 | 3 |
|
9 | 4 | /** |
10 | 5 | * Accepts an array and range. Finds the maximum sum of elements |
11 | 6 | * around the middle of the range. |
12 | | - * |
13 | | - * @param {array} array |
14 | | - * @param {number} left - the left interval of the range |
15 | | - * @param {number} middle - the middle of the range |
16 | | - * @param {number} right - the right side of the range |
17 | | - * @return {number} the maximum sum including the middle element |
| 7 | + * @private |
| 8 | + * @param {Array} array Input array. |
| 9 | + * @param {Number} left Left interval of the range. |
| 10 | + * @param {Number} middle Middle of the range. |
| 11 | + * @param {Number} right Right side of the range. |
| 12 | + * @return {Number} The maximum sum including the middle element. |
18 | 13 | */ |
19 | 14 | function crossSubarray(array, left, middle, right) { |
20 | 15 | var leftSum = -Infinity; |
|
39 | 34 | } |
40 | 35 |
|
41 | 36 | /** |
42 | | - * Using divide and conquer finds the maximum sum of subarray of the given |
43 | | - * |
44 | | - * @param {array} array |
45 | | - * @param {number} left side of the range |
46 | | - * @param {number} the right side of the range |
47 | | - * @return {number} the maximum sum of the elements of |
48 | | - * subarray whithin the given range |
| 37 | + * @private |
| 38 | + * @param {Array} array Input array. |
| 39 | + * @param {Number} left Left side of the range. |
| 40 | + * @param {Number} right Right side of the range. |
| 41 | + * @return {Number} Maximum sum of the elements of |
| 42 | + * subarray whithin the given range. |
49 | 43 | */ |
50 | 44 | function maxSubarrayPartitioner(array, left, right) { |
51 | 45 | if (right - left <= 1) { |
|
60 | 54 | } |
61 | 55 |
|
62 | 56 | /** |
63 | | - * Returns the maximum sum of the elements of a subarray of the given array |
| 57 | + * Finds the maximum sum of the elements of a subarray in a given array |
| 58 | + * using the divide and conquer algorithm by Bentley, Jon (1984). |
| 59 | + * For example, for the sequence of values -2, 1, -3, 4, -1, 2, 1, -5, 4 |
| 60 | + * the contiguous subarray with the largest sum is 4, -1, 2, 1, with sum 6. |
| 61 | + * <br><br> |
| 62 | + * Time complexity: O(N log N). |
| 63 | + * |
| 64 | + * @example |
| 65 | + * var max = require('path-to-algorithms/src/searching/'+ |
| 66 | + * 'maximum-subarray-divide-and-conquer').maxSubarray; |
| 67 | + * console.log(max([-2, 1, -3, 4, -1, 2, 1, -5, 4])); // 6 |
64 | 68 | * |
65 | | - * @param {array} the array |
66 | | - * @return the maximum sum |
| 69 | + * @public |
| 70 | + * @module searching/maximum-subarray-divide-and-conquer |
| 71 | + * @param {Array} array Input array. |
| 72 | + * @return {Number} Maximum sum of the elements of a subarray. |
67 | 73 | */ |
68 | 74 | function maxSubarray(array) { |
69 | 75 | return maxSubarrayPartitioner(array, 0, array.length); |
70 | 76 | } |
71 | 77 |
|
72 | 78 | exports.maxSubarray = maxSubarray; |
73 | 79 |
|
74 | | -}(typeof exports === 'undefined' ? window : exports)); |
| 80 | +})(typeof window === 'undefined' ? module.exports : window); |
0 commit comments