Program of count inversion C code
Example
a={9,4,5,8}
Total number of inversion needed
at index 0
9>4,5,8 so inversion needed=3
at index 1,2,3 no inversion needed so total number of inversion needed=3.
Example
a={10,9,8,7}
at index 0 inversion needed
10>9,8,7 so 3 inversion needed
at index 1
9>8,7 so 2 inversion needed
at index 2
8>7 so 1 inversion needed
total number of count inversion is=3+2+1=6
C code of count inversion
#include<stdio.h>
void main()
{
int a[5]={9,4,5,8};
int i;
int j;
int n=4;
int count=0;
for(i=0;i<n;i++)
{
for(j=1;j<n;j++)
{
if(i<j&&(a[i]>a[j]))
{
count++;
}
}
}
printf("total inversion needed %d",count);
}
Output
total inversion needed 3
0 Comments