Given a non-empty array of items. You have to move all the items from the right side to odd positions (zero-based), and from the left side — to even positions, as follows:
raw data: 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13
result: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
What in-place algorithm exists for this with O(n) time complexity? What is its implementation?
The inverse problem is solved here (this algorithm can be essentially inverted, but it will look ugly).
Here is only the algorithm itself. For details, explanations, and alternative approaches see answer for the inverse problem.
This problem is simpler than the inverse problem, referred in OP, because here we have to reorder sub-arrays starting from the larger ones, in the same order as doing cycle leader algorithm (the inverse problem has to do it separately and in reverse order, starting from the smaller ones to obtain O(N) complexity).