Count Inversions in an array indicates – how far (or close) the array is from being sorted. If array is already sorted then inversion count is 0. If array is sorted in reverse order that inversion count is the maximum.

Formally speaking, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j

Example:

The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3)

METHOD 1 (Simple)

For each element, count number of elements which are on right side of it and are smaller than it.

[ad type=”banner”]

Implementation:

Java Programming for Count Inversions in an array: 

class Test
{
static int arr[] = new int[]{1, 20, 6, 4, 5};

static int getInvCount(int n)
{
int inv_count = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i+1; j < n; j++)
if (arr[i] > arr[j])
inv_count++;

return inv_count;
}

public static void main(String[] args)
{
System.out.println("Number of inversions are " + getInvCount(arr.length));

}
}

Output:

Number of inversions are 5

Time Complexity: O(n^2)

METHOD 2(Enhance Merge Sort)

Suppose we know the number of inversions in the left half and right half of the array (let be inv1 and inv2), what kinds of inversions are not accounted for in Inv1 + Inv2? The answer is – the inversions we have to count during the merge step. Therefore, to get number of inversions, we need to add number of inversions in left subarray, right subarray and merge( ).

Count Inversions in an array

How to get number of inversions in merge()?

[ad type=”banner”]

In merge process, let i is used for indexing left sub-array and j for right sub-array. At any step in merge(), if a[i] is greater than a[j], then there are (mid – i) inversions. because left and right subarrays are sorted, so all the remaining elements in left-subarray (a[i+1], a[i+2] … a[mid]) will be greater than a[j]

Count Inversions in an array

The complete picture:

Count Inversions in an array java

Implementation:

Java Programming for Count Inversions in an array: 

[ad type=”banner”]
 // Java implementation of counting the 
// inversion using merge sort

class Test
{


static int mergeSort(int arr[], int array_size)
{
int temp[] = new int[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}


static int _mergeSort(int arr[], int temp[], int left, int right)
{
int mid, inv_count = 0;
if (right > left)
{

mid = (right + left)/2;

inv_count = _mergeSort(arr, temp, left, mid);
inv_count += _mergeSort(arr, temp, mid+1, right);


inv_count += merge(arr, temp, left, mid+1, right);
}
return inv_count;
}


static int merge(int arr[], int temp[], int left, int mid, int right)
{
int i, j, k;
int inv_count = 0;

i = left;
j = mid;
k = left;
while ((i <= mid - 1) && (j <= right))
{
if (arr[i] <= arr[j])
{
temp[k++] = arr[i++];
}
else
{
temp[k++] = arr[j++];


inv_count = inv_count + (mid - i);
}
}


while (i <= mid - 1)
temp[k++] = arr[i++];


while (j <= right)
temp[k++] = arr[j++];


for (i=left; i <= right; i++)
arr[i] = temp[i];

return inv_count;
}


public static void main(String[] args)
{
int arr[] = new int[]{1, 20, 6, 4, 5};
System.out.println("Number of inversions are " + mergeSort(arr, 5));
}
}

Output:

Number of inversions are 5

If we want to count only inversions then we need to create a copy of original array and call mergeSort() on copy.

Time Complexity: O(nlogn)

Algorithmic Paradigm: Divide and Conquer

Categorized in: