Counting inversions in an array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Counting inversions in an array is a classic problem in computer science with applications in various fields such as sorting theory, information retrieval, and computational biology. An inversion is a pair of indices (i, j) in an array such that i < j and the array element at i is greater than the array element at j. Counting these inversions helps understand how far an array is from being sorted.
Problem Definition and Importance
Given an array of n integers, the goal is to determine the number of inversions. This measure indicates the degree of disorder within the array. An inversion count of 0 means the array is sorted, while a maximum inversion count signifies a completely reversed array.
Inversions are crucial for:
- Sorting Algorithms: Understanding inversions can optimize or analyze sorting algorithms.
- Genome Analysis: Inversions correspond to genome mutations where segments are reversed.
- Rank Correlation: It is used to calculate the Kendall tau distance between two rankings.
Naive Approach
The simplest way to count inversions is by using a brute-force approach. This involves checking every pair of elements in the array.
Algorithm
- Initialize
inv_count = 0. - Iterate through the array with two loops,
ifrom0ton-2andjfromi+1ton-1. - For each pair
(i, j), ifarray[i] > array[j], incrementinv_count. - Return
inv_count.
Complexity Analysis
This approach has a time complexity of , which is inefficient for large arrays due to its quadratic nature.
Example
Consider the array [2, 4, 1, 3, 5].
- Inversions are
(2, 1),(4, 1), and(4, 3). - Total inversions = 3.
Efficient Approach: Merge Sort-Based Method
A more efficient approach utilizes a modified version of the merge sort algorithm. The key idea is to count inversions during the merge process.
Algorithm
Base Case
- If the array has one or zero elements, return 0 inversions.
Recursive Case
- Divide the array into two halves.
- Recursively count inversions in the left half, right half, and during the merge process.
- During merging, count inversions where the left element is greater than the right element, as these represent valid inversions across the divided parts.
Merge Function
While merging two sorted halves, for any element from the left half that is greater than an element from the right half, every remaining element in the left half will also be greater.
Pseudocode for Merge and Count
Complexity Analysis
The modified merge sort has a time complexity of , making it suitable for large datasets.
Example Implementation
Key Points Summary
| Aspect | Details |
| Definition | An inversion is a pair (i, j) where i < j and array[i] > array[j]. |
| Naive Approach | Time Complexity: Inefficient for large arrays. |
| Merge Sort Approach | Time Complexity: Efficient for large datasets. |
| Use Cases | Sorting optimization, genome analysis, rank correlation. |
| Example | Array [2, 4, 1, 3, 5] has 3 inversions: (2, 1), (4, 1), (4, 3). |
Conclusion
The problem of counting inversions in an array exemplifies how algorithmic efficiency significantly impacts computational tasks. While a naive approach is easy to understand, the merge sort-based method provides a much-needed performance boost for large arrays. This balance between understanding and efficiency underscores much of algorithm design and analysis within computer science.

