forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Merge_Sort.js
65 lines (60 loc) · 1.43 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function Merge(array,left,right,middle){
/*dividing in to array of n1,n2 length */
n1 = middle - left +1;
n2 = right - middle;
var L = [];
var R = [];
/*loops for filling sub arrays*/
for(var i =0;i<n1;i++){
L[i] = array[left + i ];
}
for(var j=0;j<n2;j++){
R[j]=array[middle+j+1];
}
i = 0;
j = 0 ;
var k = left;
/*loop for Merging values from 2 sub arrays to one*/
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
array[k] = L[i];
i++;
}
else
{
array[k] = R[j];
j++;
}
k++;
}
/*loops for adding remaing values*/
while (i < n1)
{
array[k] = L[i];
i++;
k++;
}
while (j < n2)
{
array[k] = R[j];
j++;
k++;
}
}
function MergeSort(array,left,right){
if(left < right){
/*finding middle of sub array*/
var middle = Math.floor((left +right)/2);
/*division in 2 sub arrays*/
MergeSort(array,left,middle);
MergeSort(array,middle+1,right);
Merge(array,left,right,middle);
}
}
/* test */
a=[1,3,6,54,56,78,78,6754,643,5,4];
MergeSort(a,0,a.length-1);
console.log(a);
/*output 1,3,4,5,6,54,56,78,78,643,6754 */