You are viewing a single comment's thread. Return to all comments →
I see that you're comparing elements that are out of place and incrementing number of swaps (ret) value. But that is not neccessarily, number of minimum of swaps required.
array1: 2 5 3 1
array2: 1 2 3 5
have 3 elements out of place. but minimum number of swaps required are 2. I would appreciate you can help me understand the algorithm.
Check it for reverse array1: 1 3 5 2 - it require two swaps. We are only taking min of ascending and descending.