Saturday, 18 November 2017

C++ Program to Count Inversion in an Array


Code:

#include  iostream

using namespace std;

int CountInversion(int a[], int n)
{
int i, j, count = 0;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
if(a[i] > a[j])
count++;
}
return count;
}

int main()
{
int n, i;
cout<<"\nEnter the number of data element: ";
cin>>n;

int arr[n];
for(i = 0; i < n; i++)
{
cout<<"Enter element "<
cin>>arr[i];
}

// Printing the number of  inversion in the array.
cout<<"\nThe number of  inversion in the array: "<

return 0;
}


Output:

Case 1:
Enter the number of data element: 10
Enter element 1: 9
Enter element 2: 3
Enter element 3: 2
Enter element 4: 6
Enter element 5: 8
Enter element 6: 4
Enter element 7: 5
Enter element 8: 7
Enter element 9: 0
Enter element 10: 1

The number of  inversion in the array: 29


More C++ Programs:














100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...