Skip to content

Commit 779c7ec

Browse files
author
Dan Kaplun
committed
Refactors src/sorting/mergesort.js
Adds optional `start` and `end` parameters to `mergeSort` Exposes `mergeSort.merge` Updates documentation
1 parent 83a8c51 commit 779c7ec

1 file changed

Lines changed: 64 additions & 87 deletions

File tree

src/sorting/mergesort.js

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

4-
var mergeSort = (function () {
4+
function compare(a, b) {
5+
return a - b;
6+
}
57

6-
function compare(a, b) {
7-
return a - b;
8+
/**
9+
* Mergesort method which is recursively called for sorting the input array.
10+
*
11+
* @public
12+
* @param {array} array The array which should be sorted
13+
* @param {Function} cmp Compares two items in an array
14+
* @param {number} start Left side of the subarray
15+
* @param {number} end Right side of the subarray
16+
* @returns {array} Array with sorted subarray
17+
*/
18+
function mergeSort(array, cmp, start, end) {
19+
cmp = cmp || compare;
20+
start = start || 0;
21+
end = end || array.length;
22+
if (Math.abs(end - start) <= 1) {
23+
return [];
824
}
25+
var middle = Math.ceil((start + end) / 2);
926

10-
/**
11-
* Mergesort method which is recursively called for sorting the input array.
12-
*
13-
* @private
14-
* @param {array} array The array which should be sorted
15-
* @param {number} start Left side of the subarray
16-
* @param {number} end Right side of the subarray
17-
* @returns {array} Array with sorted subarray
18-
*/
19-
function mergesort(array, start, end, cmp) {
20-
if (Math.abs(end - start) <= 1) {
21-
return [];
22-
}
23-
var middle = Math.ceil((start + end) / 2);
24-
25-
mergesort(array, start, middle, cmp);
26-
mergesort(array, middle, end, cmp);
27+
mergeSort(array, cmp, start, middle);
28+
mergeSort(array, cmp, middle, end);
2729

28-
return merge(array, start, middle, end, cmp);
29-
}
30+
return mergeSort.merge(array, cmp, start, middle, end);
31+
}
3032

31-
/**
32-
* Devides and sort merges two subarrays of given array
33-
*
34-
* @private
35-
* @param {array} array The array which subarrays should be sorted
36-
* @param {number} start The start of the first subarray.
37-
* This subarray is with end middle - 1.
38-
* @param {number} middle The start of the second array
39-
* @param {number} end end - 1 is the end of the second array
40-
* @returns {array} The array with sorted subarray
41-
*/
42-
function merge(array, start, middle, end, cmp) {
43-
var left = [];
44-
var right = [];
45-
var leftSize = middle - start;
46-
var rightSize = end - middle;
47-
var maxSize = Math.max(leftSize, rightSize);
48-
var size = end - start;
49-
var i;
33+
/**
34+
* Devides and sort merges two subarrays of given array
35+
*
36+
* @public
37+
* @param {array} array The array which subarrays should be sorted
38+
* @param {number} start The start of the first subarray.
39+
* This subarray is with end middle - 1.
40+
* @param {number} middle The start of the second array
41+
* @param {number} end end - 1 is the end of the second array
42+
* @returns {array} The array with sorted subarray
43+
*/
44+
mergeSort.merge = function (array, cmp, start, middle, end) {
45+
var left = [];
46+
var right = [];
47+
var leftSize = middle - start;
48+
var rightSize = end - middle;
49+
var maxSize = Math.max(leftSize, rightSize);
50+
var size = end - start;
51+
var i;
5052

51-
for (i = 0; i < maxSize; i += 1) {
52-
if (i < leftSize) {
53-
left[i] = array[start + i];
54-
}
55-
if (i < rightSize) {
56-
right[i] = array[middle + i];
57-
}
53+
for (i = 0; i < maxSize; i += 1) {
54+
if (i < leftSize) {
55+
left[i] = array[start + i];
5856
}
59-
i = 0;
60-
while (i < size) {
61-
if (left.length && right.length) {
62-
if (cmp(left[0], right[0]) > 0) {
63-
array[start + i] = right.shift();
64-
} else {
65-
array[start + i] = left.shift();
66-
}
67-
} else if (left.length) {
68-
array[start + i] = left.shift();
69-
} else {
57+
if (i < rightSize) {
58+
right[i] = array[middle + i];
59+
}
60+
}
61+
i = 0;
62+
while (i < size) {
63+
if (left.length && right.length) {
64+
if (cmp(left[0], right[0]) > 0) {
7065
array[start + i] = right.shift();
66+
} else {
67+
array[start + i] = left.shift();
7168
}
72-
i += 1;
69+
} else if (left.length) {
70+
array[start + i] = left.shift();
71+
} else {
72+
array[start + i] = right.shift();
7373
}
74-
return array;
74+
i += 1;
7575
}
76-
77-
/**
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 ]
86-
*
87-
* @public
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.
94-
*/
95-
return function (array, cmp) {
96-
cmp = cmp || compare;
97-
return mergesort(array, 0, array.length, cmp);
98-
};
99-
100-
}());
76+
return array;
77+
};
10178

10279
exports.mergeSort = mergeSort;
10380

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

0 commit comments

Comments
 (0)