To get the logic for the exchange of elements in the sample data you posted, take a piece of paper and write down the source and target elements for each swap that is done.
Then look at how the...