-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeSort.js
More file actions
39 lines (36 loc) · 905 Bytes
/
mergeSort.js
File metadata and controls
39 lines (36 loc) · 905 Bytes
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
// helper function to merge two sorted arrays
function merge(arr1, arr2) {
let i = 0,
j = 0;
const lenArr1 = arr1.length;
const lenArr2 = arr2.length;
const sortedArray = [];
while (i < lenArr1 && j < lenArr2) {
if (arr2[j] > arr1[i]) {
sortedArray.push(arr1[i]);
i++;
} else {
sortedArray.push(arr2[j]);
j++;
}
}
while (i < lenArr1) {
sortedArray.push(arr1[i]);
i++;
}
while (j < lenArr2) {
sortedArray.push(arr2[j]);
j++;
}
return sortedArray;
}
// using recursion
function mergeSort(arr) {
if (arr.length <= 1) return arr;
const mid = Math.floor(arr.length / 2);
const leftArray = mergeSort(arr.slice(0, mid));
const rightArray = mergeSort(arr.slice(mid));
return merge(leftArray, rightArray);
}
console.log(mergeSort([1, 20, 30, 50, 100, 10, 50]));
console.log(mergeSort([1, 2, 14, 99, 10, 20, 30, 50, 100]));