Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C/C++ Program to Count Inversions in an array using Merge Sort?
The inversion count in an array indicates how many changes are required to convert the array into its sorted form. When an array is already sorted, it needs 0 inversions. When the array is sorted in reverse order, the number of inversions is maximum. We can efficiently solve this problem using a modified merge sort algorithm to achieve O(n log n) time complexity.
Syntax
int merge(int arr[], int temp[], int left, int mid, int right); int mergeSort(int arr[], int temp[], int left, int right); int countInversions(int arr[], int n);
Algorithm
The approach uses a divide-and-conquer strategy similar to merge sort. During the merge step, when an element from the right array is smaller than an element from the left array, it forms inversions with all remaining elements in the left array.
Example
Here's a complete C program to count inversions using merge sort −
#include <stdio.h>
int merge(int arr[], int temp[], int left, int mid, int right) {
int i = left; /* Index for left subarray */
int j = mid + 1; /* Index for right subarray */
int k = left; /* Index for merged array */
int count = 0;
/* Merge the two arrays while counting inversions */
while (i <= mid && j <= right) {
if (arr[i] <= arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
/* All elements from i to mid are greater than arr[j] */
count += (mid - i + 1);
}
}
/* Copy remaining elements of left subarray */
while (i <= mid)
temp[k++] = arr[i++];
/* Copy remaining elements of right subarray */
while (j <= right)
temp[k++] = arr[j++];
/* Copy merged array back to original array */
for (i = left; i <= right; i++)
arr[i] = temp[i];
return count;
}
int mergeSort(int arr[], int temp[], int left, int right) {
int count = 0;
if (right > left) {
int mid = (left + right) / 2;
count += mergeSort(arr, temp, left, mid);
count += mergeSort(arr, temp, mid + 1, right);
count += merge(arr, temp, left, mid, right);
}
return count;
}
int countInversions(int arr[], int n) {
int temp[n];
return mergeSort(arr, temp, 0, n - 1);
}
int main() {
int arr[] = {1, 5, 6, 4, 20};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
int invCount = countInversions(arr, n);
printf("Number of inversions: %d\n", invCount);
return 0;
}
Array: 1 5 6 4 20 Number of inversions: 2
How It Works
- The algorithm divides the array into two halves recursively
- During the merge step, when an element from the right array is smaller than an element from the left array, it forms inversions
- The number of inversions is calculated as (mid - i + 1) since all remaining elements in the left subarray are greater
- Time complexity: O(n log n), Space complexity: O(n)
Conclusion
Using merge sort to count inversions efficiently reduces the time complexity from O(n²) to O(n log n). This approach is ideal for large arrays where performance matters.
