Under Construction: Consider this content a preview of the real thing which is coming soon (hopefully).
Prerequisites (click to view)
graph LR
ALG(["fas:fatrophy Inversion Counting fas:fatrophy #160;"])
ASY_ANA(["fas:facheck Asymptotic Analysis #160;"])
click ASY_ANA "./mathasymptoticanalysis"
MS(["fas:facheck Merge Sort #160;"])
click MS "./sortingmerge"
ASY_ANA>ALG
MS>ALG
Count the number of inversions in an array. An inversion is two items that are
out of order. For instance, in the array: [1][3][2]
the numbers 3 and 2
represent an inversion.
This algorithm is essentially the same as merge sort with the exception that it keeps track of inversions while sorting.
History
The concept of inversion dates back to 1750 when G. Cramer used them as a device for solving linear equations^{1}.
Applications
The typical use of such algorithms is comparing preferences between users. The number of inversions between user A’s ranking of products and users B’s ranking of products represents how similar their affinities are.
 Collaborative Filtering
Pseudo Code
A = input array
inversions = 0
if length of A is 1
return 0
count:
A1 = first half of A
A2 = second half of A
inversions += recursively count\sort A1
inversions += recursively count\sort A2
i = 1
j = 1
for k = 1 to n
if A1[i] < A2[j]
A[k] = A1[i]
i++
else
A[k] = A2[j]
j++
inversions += # of items remaining in A1
return inversions
Asymptotic Complexity
$O(n \log n)$
Source Code
Relevant Files:
Click here for build and run instructions
Exercises

Answer me these questions three:
a. What is your name?
b. What is your quest?
c. What… is the airspeed velocity of an unladen swallow?Answers (click to expand)
 It is Arthur, King of the Britons
 To seek the Holy Grail
 What do you mean? An African or European swallow?

Do I make you randy? Yeah, do I?
Answers (click to expand)
[Click here](listdatastructsortedarrayanswers) for the answers

The Art of Computer Programming Volume 3 Sorting and Searching p. 11 ↩