If I have a sorted array, e.g.
['A', 'A', 'A', 'B', 'B', 'B', 'B', 'B', 'C', 'C']
Is there an algorithm for turning this into:
['A', 'B', 'C', 'B', 'A', 'B', 'A', 'B', 'C', 'B']
i.e. One of the maximally unsorted permutations, when considered as a circular buffer.
There may be many maximally unsorted permutations; many will be rotations or mirrors of each other.
The "circular buffer" restriction means that the first and last positions should be considered adjacent, like any other two neighbours.
The "unsortedness" is the sum of the reciprocals of the (circular) offsets between identical elements.
For the sorted list, far above:
A: 1 + 1 + 1/8 == 2 1/8
B: 1 + 1 + 1 + 1 + 1 + 1/5 == 5 1/5
C: 1 + 1/9 == 1 1/9
-------
unsortedness == 7.436
For the unsorted list, above:
A: 1/4 + 1/2 + 1/4 == 1
B: 1/2 + 1/2 + 1/2 + 1/2 + 1/2 == 2 1/2
C: 1/6 + 1/4 == 5/12
-------
unsortedness == 3.917