-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge_sort.js
More file actions
35 lines (34 loc) Β· 973 Bytes
/
merge_sort.js
File metadata and controls
35 lines (34 loc) Β· 973 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
// Hazrat Ali
// University Of Scholars
function mergeSort(arr) {
let len = arr.length;
if (len < 2)
return arr;
let mid = Math.floor(len / 2),
left = arr.slice(0, mid),
right = arr.slice(mid);
//send left and right to the mergeSort to broke it down into pieces
//then merge those
return merge(mergeSort(left), mergeSort(right));
// Merge sort
}
function merge(left, right) {
let result = [],
lLen = left.length,
rLen = right.length,
l = 0,
r = 0;
while (l < lLen && r < rLen) {
if (left[l] < right[r]) {
result.push(left[l++]);
}
else {
result.push(right[r++]);
}
}
//remaining part needs to be addded to the result
return result.concat(left.slice(l)).concat(right.slice(r));
}
console.log(mergeSort([7, 5, 2, 4, 3, 9]));
console.log(mergeSort([9, 7, 5, 4, 3, 1]));
console.log(mergeSort([1, 2, 3, 4, 5, 6]));