forked from Prakshalshah78/DSA_Relevel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inversions_Optimised.js
64 lines (56 loc) · 1.22 KB
/
Inversions_Optimised.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
let inv = 0;
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{
//a1[p1]>a2[p2]
arr[k] = a2[p2];
p2++;
k++;
if(a1[p1]>2*a2[p2])
inv+=(m1-p1); //*
}
}
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 inv;
}
let arr = [3,5,6,9,1,2,7,8];
MergeSort(arr,0,7)
console.log(arr)