how inversion_count function gives us the value x=1 and y=3 .
Inversion count problem
@Abhishmu5 sorry for the delay due to some tech error i was not able to see your doubt.
kindly tell the time stamp where you have the doubt
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.
i am sorry , actually i was doing data science course and forget about this thing…
time stamp is 12:28 ,how inversion will do the work
@Abhishmu5 actually i cant see the video now as i have marked it resolved just create a new doubt ( some other TA or me will help you then)
should i share the code?
code
#include
using namespace std;
int merge(int *a,int s,int e)
{
int mid=(s+e)/2;
int i=s;
int j=mid+1;
int k=s;
int tmp[1000];
int cnt=0;
while(i<=mid and j<=e)
{
if(a[i]<=a[j])
{
tmp[k++]=a[i++];
}
else
{
tmp[k++]=a[j++];
cnt =cnt+mid-i+1;
}
}
//fill element if some elements are left in one of the array then we will copy the same things
while(i<=mid)
{
tmp[k++]=a[i++];
}
while(j<=e)
{
tmp[k++]=a[j++];
}
for (int i=s;i<e;i++)
{
a[i]=tmp[i];
}
return cnt;
}
int inversion_count(int *a,int s,int e)
{
//base case
if(s>=e)
{
return 0;
}
//divide
int mid=(s+e)/2;
//merge sort
int x=inversion_count(a,s,mid);
int y=inversion_count(a,mid+1,e);
int z=merge(a,s,e);
return x+y+z;
}
int main()
{
int a[]={1,5,2,6,0,20,20,205};
int n=sizeof(a)/sizeof(int);
cout<<inversion_count(a,0,n-1)<<endl;
return 0;
}
how //merge sort is working ??
@Abhishmu5 for this you must know how merge sort works so when we compare two elements in merge sort we can count the inversions too whats the problem.
if trouble in merge sort refer this prateek bhaiya video then that hint video