Skip to content

Commit e3d4bf3

Browse files
committed
Merge pull request mgechev#38 from AndreyGeonya/doc-pack14
Doc pack14
2 parents bce1e66 + 2dc73ba commit e3d4bf3

11 files changed

Lines changed: 180 additions & 89 deletions

‎src/sorting/countingsort.js‎

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

4-
/**
5-
* Counting sort algorithm. It's with complexity O(n) but it's
6-
* correct only for array of integers.
7-
*
8-
* @public
9-
*/
104
var countingSort = (function () {
115

126
/**
13-
* Gets the count of the elements into the input array
7+
* Gets the count of the elements into the input array.
148
*
159
* @private
16-
* @param {array} array The input array
17-
* @returns {array} count The count of each element from the input array
10+
* @param {Array} array The input array.
11+
* @return {Array} The count of each element from the input array.
1812
*/
1913
function getCount(array) {
2014
var count = [];
@@ -27,12 +21,12 @@
2721
}
2822

2923
/**
30-
* Gets the count of the elements which are less than a given
24+
* Gets the count of the elements which are less than a given.
3125
*
3226
* @private
33-
* @param {array} array The input array
34-
* @returns {array} less The count of the elements which
35-
* are less than each element from the input
27+
* @param {Array} array The input array.
28+
* @return {Array} less The count of the elements which.
29+
* are less than each element from the input.
3630
*/
3731
function getLessCount(array) {
3832
var less = [];
@@ -46,12 +40,12 @@
4640
}
4741

4842
/**
49-
* Sorts the input array
43+
* Sorts the input array.
5044
*
5145
* @private
52-
* @param {array} array Input which should be sorted
53-
* @param {array} less Count of the less elements for each element
54-
* @returns {array} result The sorted input
46+
* @param {Array} array Input which should be sorted.
47+
* @param {Array} less Count of the less elements for each element.
48+
* @return {Array} The sorted input.
5549
*/
5650
function sort(array, less) {
5751
var result = [];
@@ -71,11 +65,19 @@
7165
}
7266

7367
/**
74-
* Sorts a given array
68+
* Counting sort algorithm. It's correct only
69+
* for array of integers.<br><br>
70+
* Time complexity: O(N).
71+
*
72+
* @example
73+
* var sort = require('path-to-algorithms/src/' +
74+
* 'sorting/countingsort').countingSort;
75+
* console.log(sort([2, 5, 1, 3, 4])); // [ 1, 2, 3, 4, 5 ]
7576
*
7677
* @public
77-
* @param {array} array Array which should be sorted
78-
* @returns {array} array Sorted array
78+
* @module sorting/countingsort
79+
* @param {Array} array Array which should be sorted.
80+
* @return {Array} Sorted array.
7981
*/
8082
return function (array) {
8183
var less = getLessCount(getCount(array));
@@ -85,4 +87,4 @@
8587

8688
exports.countingSort = countingSort;
8789

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

‎src/sorting/heapsort.js‎

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
return a - b;
66
}
77

8-
/**
9-
* The heapsort algorithm. It's complexity is O(nlog n).
10-
*
11-
* @public
12-
*/
138
var heapSort = (function () {
149

1510
/**
1611
* Finds the correct place of given element in given max heap.
1712
*
1813
* @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.
2217
*/
2318
function heapify(array, index, heapSize, cmp) {
2419
var left = 2 * index + 1;
@@ -45,8 +40,8 @@
4540
* Builds max heap from given array.
4641
*
4742
* @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.
5045
*/
5146
function buildMaxHeap(array, cmp) {
5247
for (var i = Math.floor(array.length / 2); i >= 0; i -= 1) {
@@ -56,11 +51,23 @@
5651
}
5752

5853
/**
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 ]
6063
*
6164
* @public
62-
* @param {array} array Input array
63-
* @returns {array} array Sorted array
65+
* @module sorting/heapsort
66+
* @param {Array} array Input array.
67+
* @param {Function} cmp Optional. A function that defines an
68+
* alternative sort order. The function should return a negative,
69+
* zero, or positive value, depending on the arguments.
70+
* @return {Array} Sorted array.
6471
*/
6572
return function (array, cmp) {
6673
cmp = cmp || comparator;
@@ -80,4 +87,4 @@
8087

8188
exports.heapSort = heapSort;
8289

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

‎src/sorting/insertion-binary-sort.js‎

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,25 @@
66
}
77

88
/**
9-
* Modified version of insertionsort. It uses binary search for finding
9+
* Modified version of insertion sort. It uses binary search for finding
1010
* where the current element should be inserted. It's correct because
1111
* the binary search looks just in the first part of the array
12-
* which is actually sorted. It's complexity is O(n^2)
12+
* which is actually sorted.<br><br>
13+
* Time complexity: O(N^2).
14+
*
15+
* @example
16+
*
17+
* var sort = require('path-to-algorithms/src' +
18+
* '/sorting/insertion-binary-sort').insertionBinarySort;
19+
* console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
1320
*
1421
* @public
15-
* @param {array} array Input array
16-
* @param {array} array Sorted array
22+
* @module sorting/insertion-binary-sort
23+
* @param {Array} array Input array.
24+
* @param {Function} cmp Optional. A function that defines an
25+
* alternative sort order. The function should return a negative,
26+
* zero, or positive value, depending on the arguments.
27+
* @return {Array} Sorted array.
1728
*/
1829
function insertionBinarySort(array, cmp) {
1930
cmp = cmp || comparator;
@@ -44,4 +55,4 @@
4455

4556
exports.insertionBinarySort = insertionBinarySort;
4657

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

‎src/sorting/insertionsort.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66
}
77

88
/**
9-
* Insertionsort algorithm. It's complexity is O(n^2).
9+
* Insertionsort algorithm.<br><br>
10+
* Time complexity: O(N^2).
11+
*
12+
* @example
13+
*
14+
* var sort = require('path-to-algorithms/src' +
15+
* '/sorting/insertion-sort').insertionSort;
16+
* console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
1017
*
1118
* @public
12-
* @param {array} array Input array
13-
* @returns {array} array Sorted array
19+
* @module sorting/insertionsort
20+
* @param {Array} array Input array.
21+
* @param {Function} cmp Optional. A function that defines an
22+
* alternative sort order. The function should return a negative,
23+
* zero, or positive value, depending on the arguments.
24+
* @return {Array} Sorted array.
1425
*/
1526
function insertionSort(array, cmp) {
1627
cmp = cmp || compare;
@@ -30,4 +41,4 @@
3041

3142
exports.insertionSort = insertionSort;
3243

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

‎src/sorting/lsd.js‎

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

44
/**
5-
* Sorts strings lexicographically.
6-
* Complexity O(n*m)
5+
* Sorts strings lexicographically.<br><br>
6+
* Time complexity: O(N*M) for N keys which have M or fewer digits.
7+
*
8+
* @example
9+
*
10+
* var sort = require('../src/sorting/lsd').lsd;
11+
* // [ 'aab', 'aaa', 'acc', 'bbb', 'bcc' ]
12+
* console.log(sort(['aab', 'bbb', 'aaa', 'acc', 'bcc']));
713
*
814
* @public
9-
* @param {Array} arr Input array
10-
* @param {Number} letterIdx Index to start sorting from
11-
* @returns {Array} Sorted array
15+
* @module sorting/lsd
16+
* @param {Array} arr Array which should be sorted.
17+
* @param {Number} letterIdx Optional. Index to start sorting from.
18+
* @return {Array} Sorted array.
1219
*/
1320
function lsd(arr, letterIdx) {
1421
var temp;
@@ -41,4 +48,4 @@
4148

4249
exports.lsd = lsd;
4350

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

‎src/sorting/mergesort.js‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,22 @@
7575
}
7676

7777
/**
78-
* Initial call to the mergesort method
78+
* Merge sort algorithm.<br><br>
79+
* Time complexity: O(N log(N)).
80+
*
81+
* @example
82+
*
83+
* var sort = require('path-to-algorithms/src' +
84+
* '/sorting/mergesort').mergesortSort;
85+
* console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
7986
*
8087
* @public
81-
* @param {array} array The array which will be sorted
82-
* @returns {array} Sorted array
88+
* @module sorting/mergesort
89+
* @param {Array} array Input array.
90+
* @param {Function} cmp Optional. A function that defines an
91+
* alternative sort order. The function should return a negative,
92+
* zero, or positive value, depending on the arguments.
93+
* @return {Array} Sorted array.
8394
*/
8495
return function (array, cmp) {
8596
cmp = cmp || compare;
@@ -90,4 +101,4 @@
90101

91102
exports.mergeSort = mergeSort;
92103

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

‎src/sorting/msd.js‎

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,28 @@
3737

3838
/**
3939
* Sorts given array lexicographically.
40-
* The algorithms knows how to treat
41-
* differently length strings.
42-
* The algorithm is stable.
40+
* Algorithms knows how to treat
41+
* differently length strings.<br><br>
42+
* Algorithm is stable.
43+
* Time complexity: O(N*M) for N keys which have M or fewer digits.
4344
*
44-
* Complexity O(n*m)
45+
* @example
46+
*
47+
* var sort = require('../src/sorting/msd').msd;
48+
* // [ 'aab', 'aaa', 'acc', 'bbb', 'bcc' ]
49+
* console.log(sort(['aab', 'bbb', 'aaa', 'acc', 'bcc']));
4550
*
4651
* @public
47-
* @param {Array} arr The array, which needs to be sorted
48-
* @param {Number} d The digit from which the sorting should start
49-
* @return {Array} The sorted array
52+
* @module sorting/msd
53+
* @param {Array} arr Array which should be sorted.
54+
* @param {Number} d Optional. Digit from which sorting should start.
55+
* @return {Array} Sorted array.
5056
*/
51-
5257
function msd(arr, d) {
5358
d = d || 0;
5459
sort(arr, 0, arr.length - 1, d);
5560
return arr;
5661
}
5762

5863
exports.msd = msd;
59-
}(typeof exports === 'undefined' ? window : exports));
64+
})(typeof window === 'undefined' ? module.exports : window);

‎src/sorting/quicksort-middle.js‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
/**
2-
* Quicksort algorithm. It's with complexity O(n log(n)).
3-
* In this version of quicksort I use the middle element of the
4-
* array for pivot.
5-
*/
6-
71
(function (exports) {
82

93
'use strict';
@@ -72,8 +66,23 @@
7266
}
7367

7468
/**
75-
* Quicksort's initial point
69+
* Quicksort algorithm. In this version of quicksort used
70+
* middle element of array for the pivot.<br><br>
71+
* Time complexity: O(N log(N)).
72+
*
73+
* @example
74+
*
75+
* var sort = require('path-to-algorithms/src' +
76+
* '/sorting/quicksort-middle').quickSort;
77+
* console.log(sort([2, 5, 1, 0, 4])); // [ 0, 1, 2, 4, 5 ]
78+
*
7679
* @public
80+
* @module sorting/quicksort-middle
81+
* @param {Array} array Input array.
82+
* @param {Function} cmp Optional. A function that defines an
83+
* alternative sort order. The function should return a negative,
84+
* zero, or positive value, depending on the arguments.
85+
* @return {Array} Sorted array.
7786
*/
7887
return function (array, cmp) {
7988
cmp = cmp || compare;
@@ -85,4 +94,4 @@
8594

8695
exports.quickSort = quickSort;
8796

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

0 commit comments

Comments
 (0)