I have an array such as
A-B-C-D-E-F-G-H-I-J-K
I want to rotate it N places to the right; for example, if N=3, the array should end
I-J-K-A-B-C-D-E-F-G-H
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 A-B-F-E-D-C-G-H-I-J-K.
You could do something like the old bubble sort method.
For example:
FLIP(8,9)
FLIP(7,8)
FLIP(6,7)
FLIP(5,6)
FLIP(4,5)
FLIP(3,4)
FLIP(2,3)
FLIP(1,2)
would result in IABCDEFGHJK
then continue with
FLIP(9,10)
FLIP(8,9)
FLIP(7,8)
FLIP(6,7)
FLIP(5,6)
FLIP(4,5)
FLIP(3,4)
FLIP(2,3)
would result in IJABCDEFGHK
then continue with
FLIP(10,11)
FLIP(9,10)
FLIP(8,9)
FLIP(7,8)
FLIP(6,7)
FLIP(5,6)
FLIP(4,5)
FLIP(3,4)
would result in IJKABCDEFGH
That's what I have so far, certainly not the shortest or most elegant.
|
Posted by Larry
on 2004-04-19 16:47:15 |