Count inversions
An inversion is a pair of positions i < j with items[i] > items[j]: two items that
are out of order relative to each other. Write count_inversions(items) that returns
how many there are. A sorted list has zero.
count_inversions([2, 4, 1, 3, 5]) -> 3 (2,1) (4,1) (4,3)
count_inversions([1, 2, 3]) -> 0
count_inversions([3, 2, 1]) -> 3
The point is to practise seeing that merge sort can count while it sorts: whenever the merge takes from the right half, every remaining item in the left half is an inversion with it. A pairwise loop also passes, but one check has twenty thousand items.
def count_inversions(items):
...