Counts the number of inversions in an array