-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge-sort.js
37 lines (29 loc) · 839 Bytes
/
merge-sort.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
const mergeSort = (list) => {
const listLength = list.length;
if (listLength < 2) return list;
let mid = Math.floor(listLength / 2);
let left = list.slice(0, mid);
let right = list.slice(mid, listLength);
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
const merge = (left, right) => {
let result = [];
let leftLength = left.length, rightLength = right.length;
let i = 0, j = 0, index = 0;
while (i < leftLength && j < rightLength) {
if (left[i] < right[j]) {
result[index++] = left[i++];
} else {
result[index++] = right[j++];
}
}
if (i < leftLength) {
result = result.concat(left.slice(i, leftLength));
} else if(j < rightLength) {
result = result.concat(right.slice(j, rightLength));
}
return result;
}
module.exports = mergeSort;