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.
If the array is L elements long, you could do N times something like
FLIP(L-1, L)
FLIP(L-2, L-1)
FLIP(L-3, L-2)
...
FLIP(1,2)
and in a BASIC like language the code could be something like
FOR I=1 TO N
FOR J=L-1 DOWNTO 1
FLIP(J,J+1)
NEXT J
NEXT I
but this seems too kludgy - I think there must be a better way.
|
Posted by Oskar
on 2004-04-19 17:51:17 |