C++ Programming Code Examples C++ > Arrays and Matrices Code Examples Program to Count Inversion in an Array Program to Count Inversion in an Array 1. The number of switches required to make an array sorted is termed as inversion count. 2. Its value varies with the sorting algorithms. 3. The time complexity is O(n^2). #include<iostream> using namespace std; int CountInversion(int a[], int n) { int x, j, count = 0; for(x = 0; x < n; x++) { for(j = x+1; j < n; j++) if(a[x] > a[j]) count++; } return count; } int main() { int n, x; cout<<"\nEnter the number of data element: "; cin>>n; int arr[n]; for(x = 0; x < n; x++) { cout<<"Enter element "<<x+1<<": "; cin>>arr[x]; } // Printing the number of inversion in the array. cout<<"\nThe number of inversion in the array: "<<CountInversion(arr, n); return 0; }