I have built a solution to swap the lowest values with the highest values in a list.
With
SeedRandom[987] test = RandomSample@*Join @@ Range @@@ {{6, 10}, {56, 60}, {1, 5}, {-5, -1}} {-1, 2, 7, 8, 60, 57, 58, 10, 9, 4, -5, -3, 3, 59, 1, 5, -4, 6, -2, 56}
Then
swapPositions = PermutationReplace[ Ordering@Ordering@test, With[{len = Length@test}, Cycles@ Transpose@{Range @@ {1, Floor[len/2]}, Reverse@*Range @@ {Ceiling[len/2] + 1, len}} ] ]; Sort[test][[swapPositions]] {56, 9, 4, 3, -5, -2, -3, 1, 2, 7, 60, 58, 8, -4, 10, 6, 59, 5, 57, -1}
The largest half of the numbers have had their positions swapped with lowest half of the numbers.
However, it feels too verbose and I think Sort might be expensive in this case. Is there a built-in function or more terse method to achieve this. Of course with no loss in speed. The actual case is for list of length 100000 and more.
