-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.es6
37 lines (26 loc) · 847 Bytes
/
index.es6
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
'use strict';
const comparator = (a, b) => a - b;
const slice = Array.prototype.slice;
const left = (array) => slice.call(array, 0, array.length / 2);
const right = (array) => slice.call(array, array.length / 2);
function merge(leftList, rightList, cmp) {
let sorted = [];
while(leftList.length > 0 && rightList.length > 0) {
if (cmp(leftList[0], rightList[0]) <= 0) {
sorted.push(leftList.shift());
} else {
sorted.push(rightList.shift());
}
}
return sorted.concat(leftList, rightList);
}
export default function mergesort(array, cmp=comparator) {
let newArray = slice.call(array);
let leftList, rightList;
if (newArray.length <= 1) {
return newArray;
}
leftList = mergesort(left(newArray), cmp);
rightList = mergesort(right(newArray), cmp);
return merge(leftList, rightList, cmp);
}