Inversions count algorithm