I've got multiple pretty numbers all in a row. Each is a decimal digit.
0s are weakly attracted to 0s, 1s are attracted to 1s a little more strongly and so on until 9. I don't know why — it must be something I ate. As a result, a sort of two-way sideways sedimentation occurs until the higher values are closer to the middle and the lower values closer to the sides.
Specifically, this happens:
1) Find all instances of digits having the highest value. If there's an even number \$2p\$ of them, go to step 2a. If there's an odd number \$2q+1\$ of them, go to step 2b.
2a) Consider the \$p\$ instances on the left and the \$p\$ instances on the right. Continue to step 3.
2b) Consider the \$q\$ instances on the left of the middle instance, and the \$q\$ instances on its right. Continue to step 3.
3) Each member of the former subset will move right by swapping places with the digit directly on its right as long as this other digit is smaller, and members of the latter subset will move left in a similar fashion. All such swaps happen simultaneously. If exactly one lower-value digit is enclosed by two high-value digits (one on each side), always move this lower-value digit to the right instead.
4) Repeat step 3 until all digits of this value are directly side-by-side.
5) Repeat steps 1 to 4 for smaller and smaller values until the values are exhausted.
Here's a detailed example.
2101210121012 | begin 1201120211021 | four 2s; the two on the left move right, the two on the right move left 1021122011201 | conflict, resulting in change from 202 to 220 (see step 3); meanwhile, the two other 2s move inwards 1012122012101 | no remarks 1011222021101 | no remarks 1011222201101 | 2s are done 0111222210110 | six 1s, but exactly one on the left of the chain of 2s moves right, and exactly two on the right move left. observe 1s can never pass through 2s because 2 is not smaller than 1 (see step 3) 0111222211010 | no remarks 0111222211100 | end; 1s and 0s are done at the same time
Let's find the end state with the power of automation!
Input: an integer sequence where each element is between 0 and 9 inclusive. The sequence is of length \$3\leq n\leq10000\$.
Output: in any format, the end state that would be reached by following the instructions in the section 'Premise'. Shortcut algorithms that give you the correct answer are allowed.
Input -> output 0 -> 0 40304 -> 04430 3141592654 -> 1134596542 2101210121012 -> 0111222211100 23811463271786968331343738852531058695870911824015 -> 01111122233333333445556666777888888999887554211100