Inversions

Let $A[1 .. n]$ be an array of $n$ distinct numbers. If $% $ and $A[i] > A[j]$, then the pair $(i, j)$ is called an inversion of $A$.

1. List the five inversions of the array $\langle 2, 3, 8, 6, 1 \rangle$.
2. What array with elements from the set ${1, 2, \dots , n}$ has the most inversions? How many does it have?
3. What is the relationship between the running time of insertion sort and the number of inversions in the input array? Justify your answer.
4. Give an algorithm that determines the number of inversions in any permutation on $n$ elements in $\Theta(n \lg n)$ worst-case time. (Hint: Modify merge sort.)

1. List of Inversions

Inversions in the given array are: (1, 5), (2, 5), (3, 4), (3, 5), and (4, 5). (Note: Inversions are specified by indices of the array, not by values.)

2. Array With Most Inversions

The array with elements from the set ${1, 2, \dots , n}$ with the most inversions will have the elements in reverse sorted order, i.e. $\langle n, n - 1, \dots, 2, 1 \rangle$.

As the array has $n$ unique elements in reverse sorted order, for every unique pair of $(i, j)$, there will be an inversion. So, total number of inversion = number of ways to choose 2 distinct integer from the set ${1, 2, \dots , n}$ = $\binom n 2$ = $\frac {n( n - 1)} 2$.

3. Relationship With Insertion Sort

If we take look at the pseudocode for insertion sort with the definition of inversions in mind, we will realize that the more the number of inversions in an array, the more times the inner while loop will run. The reason being more inversions means most of the array is reverse sorted, i.e. more swaps to perform in the while loop.

So, the higher the number of inversions in an array, the longer insertion sort will take to sort the array.

4. Algorithm to Calculate Inversions

Although a hint to modify merge sort is already given, without that also we should think of divide-and-conquer algorithms whenever we see running time of $\Theta(\lg n)$.

As was done in merge sort, we need to recursively divide the array into halfs and count number of inversions in the sub-arrays. This will result in $\lg n$ steps and $\Theta(n)$ operations in each step to count the inversions. All in all a $\Theta(n \lg n)$ algorithm.