Inversion Count for 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.
The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3).
Number of inversions are 5
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().
Please refer complete article on Count Inversions in an array | Set 1 (Using Merge Sort) for more details!
- C# Program for Count set bits in an integer
- C# Program to Count number of binary strings without consecutive 1's
- C# Program for Count ways to reach the n\'th stair
- C# Program to Count trailing zeroes in factorial of a number
- Program to swap numbers using XOR operator in C#
- C# Program for Naive algorithm for Pattern Searching
- C# Program for KMP Algorithm for Pattern Searching
- C# Program to print all permutations of a given string
- C# Program for Median of two sorted arrays of same size
- C# Program for Activity Selection Problem | Greedy Algo-1
- C# Program for Subset Sum Problem | DP-25
- C# Program for Dijkstra's shortest path algorithm | Greedy Algo-7
- C# Program for Maximum sum rectangle in a 2D matrix | DP-27
- C# Program for Largest Sum Contiguous Subarray
- C# Program for Maximum size square sub-matrix with all 1s
- C# Program for Minimum number of jumps to reach end
- C# Program for Cutting a Rod | DP-13
- C# Program for Longest Palindromic Subsequence | DP-12
- C# Program for Egg Dropping Puzzle | DP-11
- C# Program for Matrix Chain Multiplication | DP-8