Now in my lecture notes in a course I'm taking I was given the following pseudo-code to Count Inversions (Using a Recursive Algorithm).
function Count_Inversions(Array)
if size(A) == 1 then return (A,0)
L = A[1,2, .... , n/2]
R = A[n+1, n+2, . , n]
(sortedL,inv_L) = Count_Inversions(L)
(sortedR,inv_R) = Count_Inversions(R)
Inv_LR = sum(FindRanks(L,R))
return (Merge(L,R),inv_L + inv_R + inv_LR)
Now I feel there is a mistake in this algorithm. Does one really need to count the Left and Right Inversions. When we calculate the Rank at every step won't those inversion be included.
For example I have an array with elements 1,6,8,3,7,4,9,2. Now I keep breaking the array till I get single elements. Then I join them (1,6) (3,8) (4,7) , (2,9) Now I'll find the rank of 1 (Left Array) in 6 (Right Array). I do them for each pair and get 3. That is the number of inversions in this step. I keep doing this. Joining them together and finding the ranks of the left array elements in the right array and adding them.
Plus as the array would be sorted in every step the Count_Inversions(L) and Count_Inversions(R) would equal 0.