forked from Prakshalshah78/DSA_Relevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSort.js
59 lines (52 loc) · 1.12 KB
/
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
47
48
49
50
51
52
53
54
55
56
57
58
59
function MergeSort(arr,start,end){
if(start>=end) return;
let mid = parseInt((start+end)/2);
MergeSort(arr,start,mid);
MergeSort(arr,mid+1,end);
merge(arr,start,mid,end);
}
function merge(arr,start,mid,end){
let m1 = mid - start + 1;
let m2 = end - mid;
let a1 = new Array(m1);
let a2 = new Array(m2);
for(let i = 0;i<m1;i++){
a1[i] = arr[start + i];
}
for(let j = 0;j<m2;j++){
a2[j] = arr[mid+1+j];
}
let p1 = 0; //a1
let p2 = 0; //a2
let k = start; //res
while(p1 < a1.length && p2 < a2.length){
if(a1[p1] < a2[p2]){
arr[k] = a1[p1];
p1++;
k++;
}
else{
arr[k] = a2[p2];
p2++;
k++;
}
}
if(p1 == a1.length){
while(p2 != a2.length){
arr[k] = a2[p2];
k++;
p2++;
}
}
if(p2 == a2.length){
while(p1 != a1.length){
arr[k] = a1[p1];
k++;
p1++;
}
}
return arr;
}
let arr = [14,33,27,10,35,19,42,44];
MergeSort(arr,0,7)
console.log(arr)