There is a function for counting inversions in the array, which requires O (n2) time:
static void Inverses(int[] A, ref int count) { count = 0; for(int i = 0; i < A.Length - 1; i++) { for (int j = i + 1; j < A.Length; j++) { if( A[i] > A[j] ) count++; } } } There is also a function of sorting the array with the divide and conquer approach, which requires O (n * log (n)) time:
static Int32[] Merge_Sort(Int32[] massive) { if (massive.Length == 1) return massive; Int32 mid_point = massive.Length / 2; return Merge(Merge_Sort(massive.Take(mid_point).ToArray()), Merge_Sort(massive.Skip(mid_point).ToArray())); } static Int32[] Merge(Int32[] mass1, Int32[] mass2) { Int32 a = 0, b = 0; Int32[] merged = new int[mass1.Length + mass2.Length]; for (Int32 i = 0; i < mass1.Length + mass2.Length; i++) { if (b < mass2.Length && a < mass1.Length) if (mass1[a] > mass2[b]) merged[i] = mass2[b++]; else //if int go for merged[i] = mass1[a++]; else if (b < mass2.Length) merged[i] = mass2[b++]; else merged[i] = mass1[a++]; } return merged; } It is necessary to implement an algorithm for counting inversions in an array with the divide-and-conquer approach, which would require O (n * log (n)) time.
Unfortunately, it is quite hard for me to understand recursion, when the first method calls another method, and the other calls the first provided.
