I have an array such as
ABCDEFGHIJK
I want to rotate it N places to the right; for example, if N=3, the array should end
IJKABCDEFGH
Assume that the only available operation is a FLIP method that can invert any portion of the array. For example, applied to the original array FLIP(3,6) would produce ABFEDCGHIJK.
A "bubble sort" algorithm will do it.
Start with the letter that is in relative position (11N) (where A is originally in relative position 0). Flip this letter with its immediate left neighbor (11N) times. (The letter moves through the array like a bubble rising to the surface). Then do the same with the letter in relative position (12N), (13N), ..., (10)
For N=4:
ABCDEFGHIJK
Flip H and its immediate neighbor on the left 7 times
HABCDEFGIJK
Flip I and its immediate neighbor on the left 7 times
HIABCDEFGJK
Flip J and its immediate neighbor on the left 7 times
HIJABCDEFGK
Flip K and its immediate neighbor on the left 7 times
Edited on April 19, 2004, 5:58 pm

Posted by Penny
on 20040419 17:52:44 