For a given binary array like {0, 1, 1, 0,..}
, I need to find the shortest permutation, sorting it in standard order {0,0,...,1,1,..}
.
There is a warning in documentations for FindPermutation
:
For expressions containing repeated parts, the permutation is not uniquely defined
And it's easily to find the next example:
data = {1, 1, 0};
FindPermutation[data, Sort@data]
(*Don't confuse it with FindPermutation@data,
it's not the same!*)
Output: Cycles[{{1, 2, 3}}]
(*Also:*)
PermutationCycles@Ordering@data
Output: Cycles[{{1, 3, 2}}]
But:
Permute[data, Cycles[{{1, 3}}]]
Output: {0, 1, 1}
So FindPermutation
produces not shortest result. And how to find shortest not manually?
Is it possible to find all that produce the same result?
PermutationCycles@Ordering@arr
, but does this guarantee shortest result? $\endgroup$Ordering
is "stable" in the sense that equal elements won't get swapped. So it should give a minimal permutation (he said). $\endgroup$