I'm trying to rotate a List to the right by a certain specified numbers of places. I think that an array (or List) rotation can be seen as a circular, which means elements that fall off the end would wrap around to the beginning, and vice versa. A good example of this could possibly be if we have an array or list, then we rotated it to the right by three places, the result will be as follow:
Initial Array (or List): 20, 30, 40, 50, 60, 70
Rotated to the right by 3 places: 50, 60, 70, 20, 30, 40.
I've written some code down, based of what the little I understand of this concept. I would like to know, what's the correct way to do this manually (without any sort of fancy code nor LINQ) as it will help me to understand it better.
i'm asking for the manual way to solve this problem, not with anything fancy.
public void Test8(List<int> items, int places)
{
int[] copy = new int[items.Count];
items.CopyTo(copy, 0);
for (int i = 0; i < items.Count; i++)
{
int RotateRight = (i + places) % items.Count;
items[i] = copy[RotateRight];
}
}