|
| 1 | +//Count Inversion problem with mergesort algorithm where i < j and arr[j] < arr[i] is an inversion |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace CountInversionsProblem |
| 5 | +{ |
| 6 | + class Count_Inversions |
| 7 | + { |
| 8 | + //The merge step to sort the elements and count inversions |
| 9 | + private static int Merge(int[] arr, int[] temp, int leftStart, int mid, int rightEnd) |
| 10 | + { |
| 11 | + int left = leftStart, right = mid + 1, index = left, count = 0; |
| 12 | + //Compare Elments from arr portion and sort them in temproray array temp |
| 13 | + while (left < mid + 1 && right <= rightEnd) |
| 14 | + { |
| 15 | + if (arr[right] < arr[left]) |
| 16 | + { |
| 17 | + temp[index] = arr[right++]; |
| 18 | + count += mid + 1 - left; |
| 19 | + } |
| 20 | + else |
| 21 | + { |
| 22 | + temp[index] = arr[left++]; |
| 23 | + } |
| 24 | + index++; |
| 25 | + } |
| 26 | + // if the arr porion sorted but i didn't reach the last element place the rest in temp |
| 27 | + for (; left <= mid; left++) |
| 28 | + temp[index++] = arr[left]; |
| 29 | + // if the arr porion sorted but j didn't reach the last element place the rest in temp |
| 30 | + for (; right <= rightEnd; right++) |
| 31 | + temp[index++] = arr[right]; |
| 32 | + //Place the sorted elements from temp into arr |
| 33 | + for (left = leftStart; left <= rightEnd; left++) |
| 34 | + arr[left] = temp[left]; |
| 35 | + |
| 36 | + return count; |
| 37 | + } |
| 38 | + // recursive part in merge sort to break the array into smaller portions and count inversions |
| 39 | + private static int CountInversions(int[] arr, int[] temp, int leftStart, int rightEnd) |
| 40 | + { |
| 41 | + if (leftStart < rightEnd) |
| 42 | + { |
| 43 | + int count = 0; |
| 44 | + int mid = leftStart + (rightEnd - leftStart) / 2; |
| 45 | + count += CountInversions(arr, temp, leftStart, mid); |
| 46 | + count += CountInversions(arr, temp, mid + 1, rightEnd); |
| 47 | + count += Merge(arr, temp, leftStart, mid, rightEnd); |
| 48 | + return count; |
| 49 | + } |
| 50 | + return 0; |
| 51 | + |
| 52 | + } |
| 53 | + //Limit the number of parameters |
| 54 | + public static int CountInversions(int[] arr) |
| 55 | + { |
| 56 | + int count = CountInversions(arr, new int[arr.Length], 0, arr.Length - 1); |
| 57 | + return count; |
| 58 | + } |
| 59 | + static void Main(string[] args) |
| 60 | + { |
| 61 | + Console.Write("Enter array size: "); |
| 62 | + int n = Convert.ToInt32(Console.ReadLine()); |
| 63 | + int[] arr = new int[n]; |
| 64 | + Console.WriteLine("Enter array Elements: "); |
| 65 | + for (int i = 0; i < n; i++) |
| 66 | + { |
| 67 | + arr[i] = Convert.ToInt32(Console.ReadLine()); |
| 68 | + } |
| 69 | + int count = CountInversions(arr); |
| 70 | + Console.WriteLine($"\n\nNumber of inversions = {count}"); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | +/* |
| 75 | +Sample Input |
| 76 | +
|
| 77 | +
|
| 78 | +Enter array size: 5 |
| 79 | +Enter arry elements : |
| 80 | +2 |
| 81 | +1 |
| 82 | +3 |
| 83 | +1 |
| 84 | +2 |
| 85 | +
|
| 86 | +input array : 2 1 3 1 2 |
| 87 | +
|
| 88 | +number of inversions output : 4 |
| 89 | +Complexity: |
| 90 | +Merge sort takes time complexity of O(nlogn) and space complexity of O(n) |
| 91 | + |
| 92 | + */ |
| 93 | + |
0 commit comments