Skip to content

Commit a0fc935

Browse files
committed
Merge pull request mgechev#32 from AndreyGeonya/doc-pack11
Doc pack11
2 parents 003427e + 18968af commit a0fc935

5 files changed

Lines changed: 116 additions & 58 deletions

File tree

‎src/searching/knuth-morris-pratt.js‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,24 @@
2727
return res;
2828
}
2929

30+
/**
31+
* Knuth–Morris–Pratt algorithm. Searches for the position of
32+
* the first occurrence of a specified value in a string.
33+
*
34+
* @example
35+
*
36+
* var indexOf = require('path-to-algorithm/src/searching/'+
37+
* 'knuth-morris-pratt').kmp;
38+
* console.log(indexOf('hello', 'll')); // 2
39+
*
40+
* @public
41+
* @module searching/knuth-morris-pratt
42+
* @param {String} str String.
43+
* @param {String} substr Substring.
44+
* @return {Number} A Number, representing the position
45+
* where the specified substring occurs for the first
46+
* time, or -1 if it never occurs.
47+
*/
3048
function indexOf(str, substr) {
3149
var table = builtKMPTable(substr);
3250
var i = 0;
@@ -54,4 +72,4 @@
5472

5573
exports.kmp = kmp;
5674

57-
}(typeof exports === 'undefined' ? window : exports));
75+
})(typeof window === 'undefined' ? module.exports : window);

‎src/searching/longest-increasing-subsequence.js‎

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
(function (exports) {
22
'use strict';
33

4-
/**
5-
* Algorithm from dynamic programming.
6-
* It finds the longest sub-sequence of
7-
* increasing numbers. It is not required
8-
* the numbers to be neighboring.
9-
*
10-
* Example:
11-
* 1,5,2
12-
* The longest sub-sequence is 1,2.
13-
*/
144
exports.longestSubsequence = (function () {
155

166
/**
177
* Find the index of the first largest element in array.
18-
* Complexity O(n).
8+
* Complexity: O(N).
199
*
20-
* @param {Array} array The array in which the largest
21-
* element should be found
22-
* @param {Function} cmp Function used for comparison
23-
* @return {number} The index of the first largest element
10+
* @private
11+
* @param {Array} array The array in which the largest
12+
* element should be found.
13+
* @param {Function} cmp Function used for comparison.
14+
* @return {Number} index of the first largest element
2415
*/
2516
function max(array, cmp) {
2617
if (!array || !array.length) {
@@ -40,6 +31,7 @@
4031

4132
/**
4233
* Default comparison method.
34+
* @private
4335
*/
4436
function cmp(a, b) {
4537
return a.distance - b.distance;
@@ -48,11 +40,11 @@
4840
/**
4941
* Creates directed graph from given array.
5042
* Each element's neighbours are the elements which can be
51-
* after the element in the resulting sequence.
52-
* Complexity O(n^2).
53-
*
54-
* @param {Array} array The input array
55-
* @return {Object} Graph represented with list of neighbours
43+
* after the element in the resulting sequence.<br><br>
44+
* Complexity: O(N^2).
45+
* @private
46+
* @param {Array} array The input array.
47+
* @return {Object} Graph represented with list of neighbours.
5648
*/
5749
function buildDag(array) {
5850
var result = [];
@@ -68,12 +60,12 @@
6860
}
6961

7062
/**
71-
* Finds the longest sub-sequence for given node.
72-
* O(n^n).
73-
*
63+
* Finds the longest sub-sequence for given node.<br><br>
64+
* Complexity: O(N^N).
65+
* @private
7466
* @param {Object} dag Graph represented with list of neighbours.
7567
* @param {number} node The current node.
76-
* @return {object} The longest sub-sequence for given node.
68+
* @return {object} The longest sub-sequence for given node.
7769
*/
7870
function find(dag, node) {
7971
node = node || 0;
@@ -106,6 +98,22 @@
10698
return result;
10799
}
108100

101+
/**
102+
* Algorithm from dynamic programming. It finds the longest
103+
* sub-sequence of increasing numbers. It is not required
104+
* the numbers to be neighboring. For example for 1, 5, 2
105+
* sequence the longest sub-sequence is 1, 2.
106+
*
107+
* @example
108+
* var subsequence = require('path-to-algorithms/src/searching/'+
109+
* 'longest-increasing-subsequence').longestSubsequence;
110+
* console.log(subsequence([1, 0, 4, 3, 5])); // 1, 4, 5
111+
*
112+
* @public
113+
* @module searching/longest-increasing-subsequence
114+
* @param {Array} array Input sequence.
115+
* @return {Array} Longest increasing subsequence.
116+
*/
109117
return function (array) {
110118
var results = [];
111119
var dag = buildDag(array);
@@ -124,4 +132,4 @@
124132
};
125133
})();
126134

127-
}(typeof exports === 'undefined' ? exports : this));
135+
})(typeof window === 'undefined' ? module.exports : window);

‎src/searching/maximum-subarray-divide-and-conquer.js‎

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
1-
/**
2-
* Finds the maximum subarray using the divide and conquer algorithm
3-
* by Bentley, Jon (1984) (complexity O(n(logn)));
4-
*/
5-
61
(function (exports) {
72
'use strict';
83

94
/**
105
* Accepts an array and range. Finds the maximum sum of elements
116
* 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.
1813
*/
1914
function crossSubarray(array, left, middle, right) {
2015
var leftSum = -Infinity;
@@ -39,13 +34,12 @@
3934
}
4035

4136
/**
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.
4943
*/
5044
function maxSubarrayPartitioner(array, left, right) {
5145
if (right - left <= 1) {
@@ -60,15 +54,27 @@
6054
}
6155

6256
/**
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
6468
*
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.
6773
*/
6874
function maxSubarray(array) {
6975
return maxSubarrayPartitioner(array, 0, array.length);
7076
}
7177

7278
exports.maxSubarray = maxSubarray;
7379

74-
}(typeof exports === 'undefined' ? window : exports));
80+
})(typeof window === 'undefined' ? module.exports : window);

‎src/searching/maximum-subarray.js‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
'use strict';
33

44
/**
5-
* Finds the maximum sum of subarray's element of given array
6-
* using the Kadane's algorithm
7-
* It's complexity is O(n). The algorithm can be found here:
8-
* https://en.wikipedia.org/wiki/Maximum_subarray_problem#Kadane.27s_algorithm
5+
* Finds the maximum sum of the elements of a subarray in a given array
6+
* using the Kadane's algorithm.
7+
* For example, for the sequence of values -2, 1, -3, 4, -1, 2, 1, -5, 4
8+
* the contiguous subarray with the largest sum is 4, -1, 2, 1, with sum 6.
9+
* <br><br>
10+
* Time complexity: O(N).
11+
*
12+
* @example
13+
* var max = require('path-to-algorithms/src/searching/'+
14+
* 'maximum-subarray').maxSubarray;
15+
* console.log(max([-2, 1, -3, 4, -1, 2, 1, -5, 4])); // 6
916
*
1017
* @public
11-
* @param {array} array Input array
12-
* @returns {number} max The maximum sum of
13-
* the elements of subarray of the input
18+
* @module searching/maximum-subarray
19+
* @param {Array} array Input array.
20+
* @return {Number} Maximum sum of the elements of a subarray.
1421
*/
1522
function maxSubarray(array) {
1623
var currentMax = 0;
@@ -25,4 +32,4 @@
2532

2633
exports.maxSubarray = maxSubarray;
2734

28-
}(typeof exports === 'undefined' ? window : exports));
35+
})(typeof window === 'undefined' ? module.exports : window);

‎src/searching/quickselect.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
(function (exports) {
22
'use strict';
33

4-
// O(n)
4+
/**
5+
* Returns the n-th smallest element of list within
6+
* lo..hi inclusive (i.e. lo <= n <= hi).<br><br>
7+
* Time complexity: O(N).
8+
*
9+
* @example
10+
*
11+
* var quickselect = require('path-to-algorithms/src/searching/'+
12+
* 'quickselect').quickselect;
13+
* var result = quickselect([5, 1, 2, 2, 0, 3], 1, 0, 5);
14+
* console.log(result); // 1
15+
*
16+
* @public
17+
* @module searching/quickselect
18+
* @param {Array} arr Input array.
19+
* @param {Number} n A number of an element.
20+
* @param {Number} lo Low index.
21+
* @param {Number} lo High index.
22+
* @return Returns n-th smallest element.
23+
*/
524
function quickselect(arr, n, lo, hi) {
625
function partition(arr, lo, hi, pivotIdx) {
726
function swap(arr, i, j) {
@@ -45,4 +64,4 @@
4564
}
4665
exports.quickselect = quickselect;
4766

48-
}(typeof exports === 'undefined' ? window : exports));
67+
})(typeof window === 'undefined' ? module.exports : window);

0 commit comments

Comments
 (0)