-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmerge-sort-bottom-up.js
73 lines (67 loc) · 2.17 KB
/
merge-sort-bottom-up.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/**
* @module lib/merge-sort-bottom-up
* @license MIT Copyright 2014 Daniel Imms (http://www.growingwiththeweb.com)
*/
'use strict';
var defaultCompare = require('./common/default-compare');
/**
* Merge a portion of the array.
* @param {Array} array The array to merge.
* @param {number} leftPosition The starting index of the array to merge.
* @param {number} chunkSize The size of the portion of the array to merge.
* @param {Array} workArray A work array the size of the array argument, this
* parameter is for performance reasons so many arrays aren't created by the
* algorithm.
* @param {function} compare The compare function.
* @returns The sorted array.
*/
function bottomUpMerge(
array, leftPosition, chunkSize, workArray, compare) {
var i;
var rightPosition = leftPosition + chunkSize;
var endPosition = Math.min(leftPosition + chunkSize * 2 - 1,
array.length - 1);
var leftIndex = leftPosition;
var rightIndex = rightPosition;
for (i = 0; i <= endPosition - leftPosition; i++) {
if (leftIndex < rightPosition &&
(rightIndex > endPosition ||
compare(array[leftIndex], array[rightIndex]) <= 0)) {
workArray[i] = array[leftIndex++];
} else {
workArray[i] = array[rightIndex++];
}
}
for (i = leftPosition; i <= endPosition; i++) {
array[i] = workArray[i - leftPosition];
}
}
/**
* Sorts an array using bottom up merge sort.
* @param {Array} array The array to sort.
* @param {function} compare The compare function.
* @returns The sorted array.
*/
function sort(array, compare) {
var workArray = new Array(array.length);
var chunkSize = 1;
while (chunkSize < array.length) {
var i = 0;
while (i < array.length - chunkSize) {
bottomUpMerge(array, i, chunkSize, workArray, compare);
i += chunkSize * 2;
}
chunkSize *= 2;
}
return array;
}
/**
* Sorts an array using bottom up merge sort.
* @param {Array} array The array to sort.
* @param {function} customCompare A custom compare function.
* @returns The sorted array.
*/
module.exports = function (array, customCompare) {
var compare = customCompare || defaultCompare;
return sort(array, compare);
};