-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathmergeSort.js
More file actions
39 lines (32 loc) · 1.21 KB
/
mergeSort.js
File metadata and controls
39 lines (32 loc) · 1.21 KB
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
function merge(leftArr, rightArr) {
let leftInd = 0, rightInd = 0;
const sortedArr = [];
while(leftInd < leftArr.length && rightInd < rightArr.length) {
if (leftArr[leftInd] < rightArr[rightInd]) {
sortedArr.push(leftArr[leftInd]);
leftInd++;
} else {
sortedArr.push(rightArr[rightInd]);
rightInd++;
}
}
// join leftover values from left array or right array
return sortedArr.concat(leftArr.slice(leftInd)).concat(rightArr.slice(rightInd));
}
const mergeSort = (arr) => {
if (arr.length <= 1) {
return arr;
}
const mid = Math.floor(arr.length / 2);
const leftArr = mergeSort(arr.slice(0, mid));
const rightArr = mergeSort(arr.slice(mid));
return merge(leftArr, rightArr);
};
console.log(mergeSort([4,12,5,3,78,12,133,32, 1000, 4000]));
console.log(mergeSort([14, 1, 10, 2, 3, 5, 6, 4, 7, 11, 12, 13]));
console.log(mergeSort([]));
console.log(mergeSort([1]));
console.log(mergeSort([2, 1]));
console.log(mergeSort([1,7,2,3,4,1,10,2,3,4,5]));
console.log(mergeSort(["One Piece", "One-Punch Man", "My Hero Academia", "Jujutsu Kaisen", "Death Note", "Fullmetal Alchemist: Brotherhood", "Akame ga Kill", "Bleach", "Black Clover"]));
module.exports.mergeSort = mergeSort;