-
Notifications
You must be signed in to change notification settings - Fork 25
/
Counting_inversions_using mergesort.cpp
64 lines (60 loc) · 1.31 KB
/
Counting_inversions_using mergesort.cpp
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
#include <bits/stdc++.h>
using namespace std;
int merge(int arr[],int low,int high,int m,int t[]){
int i=low,j=m;
int k=low; //to store values in resultant merged array
int c=0;
while(j<=high){
if(i==m) break;
//if element in left sub-array is smaller than simply placing it in sorted array
if(arr[i]<=arr[j]){
t[k]=arr[i];
k++;
i++;
}
//else placing element from right sub array in sorted array and updating the inversion count
else{
t[k]=arr[j];
k++;
j++;
c+=(m-i);
}
}
while(1){
if(i==m) break;
t[k]=arr[i];
k++;
i++;
}
while(j<=high){
t[k]=arr[j];
k++;
j++;
}
for(i=low;i<=high;i++)
arr[i]=t[i];
return c;
}
int count_inv(int arr[],int low,int high,int t[]){
int m,a=0,b=0,c=0,inv=0;
if(high>low){
m=(high+low)/2;
a+=count_inv(arr,low,m,t); //applying merge sort and countin inversions for left sub-array
b+=count_inv(arr,m+1,high,t); //applying merge sort and countin inversions for right sub-array
c+=merge(arr,low,high,m+1,t); //merging left and right sub-array and counting inversions while doing that
inv+=a+b+c; //summing up all the inversions
}
return inv;
}
//Main Driver Function
int main(){
int n;
cin>>n;
int a[n];
int *t = (int *)malloc(sizeof(int)*n);
for (int i=0;i<n;i++){
cin>>a[i];
}
cout<<count_inv(a,0,n-1,t);
cout<<endl;
}