-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmergesort.js
46 lines (44 loc) · 978 Bytes
/
mergesort.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
function mergeSort(array) {
if(!array.length) {
return array;
}
var arraySplit = split(array);
var arr1 = arraySplit[0];
var arr2 = arraySplit[1];
if(arr1.length === 1 && arr2.length === 1) {
return merge(arr1, arr2);
}
else {
if(arr1.length !== 1) {
arr1 = mergeSort(arr1);
}
if(arr2.length !== 1) {
arr2 = mergeSort(arr2);
}
}
return merge(arr1, arr2);
}
function split(array) {
var splitIdx = array.length / 2;
var splitIdx = Math.ceil(splitIdx);
return [array.slice(0, splitIdx), array.slice(splitIdx)];
}
function merge(arr1, arr2) {
var combined = [];
while(arr1.length && arr2.length) {
if (arr1[0] < arr2[0]) {
combined.push(arr1.shift());
} else if (arr1[0] > arr2[0]) {
combined.push(arr2.shift());
} else {
combined.push(arr1.shift());
combined.push(arr2.shift());
}
}
if (arr1.length) {
combined = combined.concat(arr1);
} else if (arr2.length) {
combined = combined.concat(arr2);
}
return combined;
}