implemented inversion counting