Consider the array {1,5,4,3,2,6,7}. In this, the number of inversions is 6. But, actually, we need only 2 swaps to get the array totally sorted i.e., (5,2), (4,3). Can someone explain this situation? Am I understanding the meaning of inversions of array in the wrong sense or is there some intricacy which I am missing. Help would be very much appreciated. Thanks in advance.

If you consider swapping only adjacent elements then you have to do 6 swaps to get your array in sorted order.

1 Like

Which condition does the concept â€śInversions in an arrayâ€ť refer to ? Is it â€śswapping only adjacent elementsâ€ť or â€śswapping any 2 elementsâ€ť ??

Can someone help me with this please?

â€śswap adjacent elementsâ€ť

1 Like

Actually the definition of inversions has nothing to do with swapping. An inversion is a pair of indices (i,j), where i < j and a[i] > a[j]. So in your array we have the six inversions (1,2), (1,3), (1,4), (2,3), (2, 4), and (3, 4).

There is an alterative interpretation as the number of adjacent swaps. But hruday968 already mentioned that.

1 Like

Thank you so much.

Thanks a lot.!