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)

[ad type=”banner”]

METHOD 1 (Simple)
For each element, count number of elements which are on right side of it and are smaller than it.

C Programming

[pastacode lang=”c” manual=”%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0Aint%20getInvCount(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20int%20inv_count%20%3D%200%3B%0A%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%20-%201%3B%20i%2B%2B)%0A%20%20%20%20for%20(int%20j%20%3D%20i%2B1%3B%20j%20%3C%20n%3B%20j%2B%2B)%0A%20%20%20%20%20%20if%20(arr%5Bi%5D%20%3E%20arr%5Bj%5D)%0A%20%20%20%20%20%20%20%20inv_count%2B%2B%3B%0A%20%0A%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F*%20Driver%20progra%20to%20test%20above%20functions%20*%2F%0Aint%20main(int%20argv%2C%20char**%20args)%0A%7B%0A%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%2020%2C%206%2C%204%2C%205%7D%3B%0A%20%20int%20n%20%3D%20sizeof(arr)%2Fsizeof(arr%5B0%5D)%3B%0A%20%20printf(%22%20Number%20of%20inversions%20are%20%25d%20%5Cn%22%2C%20getInvCount(arr%2C%20n))%3B%0A%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

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:

C Programming

[ad type=”banner”] [pastacode lang=”c” manual=”%23include%20%3Cbits%2Fstdc%2B%2B.h%3E%0A%20%0Aint%20%20_mergeSort(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20right)%3B%0Aint%20merge(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20mid%2C%20int%20right)%3B%0A%20%0A%2F*%20This%20function%20sorts%20the%20input%20array%20and%20returns%20the%0A%20%20%20number%20of%20inversions%20in%20the%20array%20*%2F%0Aint%20mergeSort(int%20arr%5B%5D%2C%20int%20array_size)%0A%7B%0A%20%20%20%20int%20*temp%20%3D%20(int%20*)malloc(sizeof(int)*array_size)%3B%0A%20%20%20%20return%20_mergeSort(arr%2C%20temp%2C%200%2C%20array_size%20-%201)%3B%0A%7D%0A%20%0A%2F*%20An%20auxiliary%20recursive%20function%20that%20sorts%20the%20input%20array%20and%0A%20%20returns%20the%20number%20of%20inversions%20in%20the%20array.%20*%2F%0Aint%20_mergeSort(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20right)%0A%7B%0A%20%20int%20mid%2C%20inv_count%20%3D%200%3B%0A%20%20if%20(right%20%3E%20left)%0A%20%20%7B%0A%20%20%20%20%2F*%20Divide%20the%20array%20into%20two%20parts%20and%20call%20_mergeSortAndCountInv()%0A%20%20%20%20%20%20%20for%20each%20of%20the%20parts%20*%2F%0A%20%20%20%20mid%20%3D%20(right%20%2B%20left)%2F2%3B%0A%20%0A%20%20%20%20%2F*%20Inversion%20count%20will%20be%20sum%20of%20inversions%20in%20left-part%2C%20right-part%0A%20%20%20%20%20%20and%20number%20of%20inversions%20in%20merging%20*%2F%0A%20%20%20%20inv_count%20%20%3D%20_mergeSort(arr%2C%20temp%2C%20left%2C%20mid)%3B%0A%20%20%20%20inv_count%20%2B%3D%20_mergeSort(arr%2C%20temp%2C%20mid%2B1%2C%20right)%3B%0A%20%0A%20%20%20%20%2F*Merge%20the%20two%20parts*%2F%0A%20%20%20%20inv_count%20%2B%3D%20merge(arr%2C%20temp%2C%20left%2C%20mid%2B1%2C%20right)%3B%0A%20%20%7D%0A%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F*%20This%20funt%20merges%20two%20sorted%20arrays%20and%20returns%20inversion%20count%20in%0A%20%20%20the%20arrays.*%2F%0Aint%20merge(int%20arr%5B%5D%2C%20int%20temp%5B%5D%2C%20int%20left%2C%20int%20mid%2C%20int%20right)%0A%7B%0A%20%20int%20i%2C%20j%2C%20k%3B%0A%20%20int%20inv_count%20%3D%200%3B%0A%20%0A%20%20i%20%3D%20left%3B%20%2F*%20i%20is%20index%20for%20left%20subarray*%2F%0A%20%20j%20%3D%20mid%3B%20%20%2F*%20j%20is%20index%20for%20right%20subarray*%2F%0A%20%20k%20%3D%20left%3B%20%2F*%20k%20is%20index%20for%20resultant%20merged%20subarray*%2F%0A%20%20while%20((i%20%3C%3D%20mid%20-%201)%20%26%26%20(j%20%3C%3D%20right))%0A%20%20%7B%0A%20%20%20%20if%20(arr%5Bi%5D%20%3C%3D%20arr%5Bj%5D)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bi%2B%2B%5D%3B%0A%20%20%20%20%7D%0A%20%20%20%20else%0A%20%20%20%20%7B%0A%20%20%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bj%2B%2B%5D%3B%0A%20%0A%20%20%20%20%20%2F*this%20is%20tricky%20–%20see%20above%20explanation%2Fdiagram%20for%20merge()*%2F%0A%20%20%20%20%20%20inv_count%20%3D%20inv_count%20%2B%20(mid%20-%20i)%3B%0A%20%20%20%20%7D%0A%20%20%7D%0A%20%0A%20%20%2F*%20Copy%20the%20remaining%20elements%20of%20left%20subarray%0A%20%20%20(if%20there%20are%20any)%20to%20temp*%2F%0A%20%20while%20(i%20%3C%3D%20mid%20-%201)%0A%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bi%2B%2B%5D%3B%0A%20%0A%20%20%2F*%20Copy%20the%20remaining%20elements%20of%20right%20subarray%0A%20%20%20(if%20there%20are%20any)%20to%20temp*%2F%0A%20%20while%20(j%20%3C%3D%20right)%0A%20%20%20%20temp%5Bk%2B%2B%5D%20%3D%20arr%5Bj%2B%2B%5D%3B%0A%20%0A%20%20%2F*Copy%20back%20the%20merged%20elements%20to%20original%20array*%2F%0A%20%20for%20(i%3Dleft%3B%20i%20%3C%3D%20right%3B%20i%2B%2B)%0A%20%20%20%20arr%5Bi%5D%20%3D%20temp%5Bi%5D%3B%0A%20%0A%20%20return%20inv_count%3B%0A%7D%0A%20%0A%2F*%20Driver%20program%20to%20test%20above%20functions%20*%2F%0Aint%20main(int%20argv%2C%20char**%20args)%0A%7B%0A%20%20int%20arr%5B%5D%20%3D%20%7B1%2C%2020%2C%206%2C%204%2C%205%7D%3B%0A%20%20printf(%22%20Number%20of%20inversions%20are%20%25d%20%5Cn%22%2C%20mergeSort(arr%2C%205))%3B%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

Number of inversions are 5

Note that above code modifies (or sorts) the input array. 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: