forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inversion.cpp
57 lines (54 loc) · 1.25 KB
/
inversion.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
//finding the count inverstion of an array using merge sort
#include <iostream>
using namespace std;
int n,temp[100];
int merge_sort(int arr[],int left ,int right);
int merge(int arr[],int left,int mid,int right);
int main()
{
int *arr,n,i;
cout<<"enter the number of elements in the array"<<endl;
cin>>n;
arr=new int[n];
for(i=0;i<n;i++)
{
cout<<"enter the element"<<endl;
cin>>arr[i];
}
int ic=merge_sort(arr,0,n-1);
cout<<ic;
}
//this function keeps partitioning the array and sorts them as well
int merge_sort(int arr[],int left ,int right)
{
int c=0;
if(right>left)//we continue dividing till right is more than the left index
{
int mid=(right+left)/2;
c+=merge_sort(arr,left,mid);//the inversion count is the sum of inversions in left
c+=merge_sort(arr,mid+1,right);//half,right half and merged array
c+=merge(arr,left,mid,right);
}
return c;
}
//this function merges the two parts and returns the inversion count
int merge(int arr[],int left,int mid,int right)
{
int c=0;
int i=left,j=mid+1,k=left;
while(i<=mid && j<=right)
{
if(arr[i]<=arr[j])
temp[k++]=arr[i++];
else
{
temp[k++]=arr[j++];
c+=mid-i+1;
}
}
while(i<=mid)
temp[k++]=arr[i++];
while(j<=right)
temp[k++]=arr[j++];
return c;
}