# (anti)clockwise rotation of array elements...HELP!

Ken Ng

Greenhorn

Posts: 26

posted 12 years ago

hi all, need some help!

I'm designing a method that can rotate array elements clockwise or anticlockwise some no. of times and return the result.

public int[] rotate(int[] array, int num_rotation, boolean clockwise)

Take for instance, int[] a = {1, 2, 3, 4, 5};

ANTICLOCKWISE

i.e last element become first, the rest push from L to R in an anticlockwise fasion

1st clockwise rotation => result: {5, 1, 2, 3, 4}

2nd clockwise rotation => result: {4, 5, 1, 2, 3}

and so on..

----------------------------------------------------------------------

CLOCKWISE

i.e first element become last, the rest push from R to L in an clockwise fashion

1st anti-clockwise rotation => result: {2, 3, 4, 5, 1}

2nd anti-clockwise rotation => result: {3, 4, 5, 1, 2}

and so on..

Can't figure the algorithm for rotation

Is there some better way of doing it.

Thanks in adv

I'm designing a method that can rotate array elements clockwise or anticlockwise some no. of times and return the result.

public int[] rotate(int[] array, int num_rotation, boolean clockwise)

Take for instance, int[] a = {1, 2, 3, 4, 5};

ANTICLOCKWISE

i.e last element become first, the rest push from L to R in an anticlockwise fasion

1st clockwise rotation => result: {5, 1, 2, 3, 4}

2nd clockwise rotation => result: {4, 5, 1, 2, 3}

and so on..

----------------------------------------------------------------------

CLOCKWISE

i.e first element become last, the rest push from R to L in an clockwise fashion

1st anti-clockwise rotation => result: {2, 3, 4, 5, 1}

2nd anti-clockwise rotation => result: {3, 4, 5, 1, 2}

and so on..

Can't figure the algorithm for rotation

Is there some better way of doing it.

Thanks in adv

Dirk Schreckmann

Sheriff

Posts: 7023

Vicken Karaoghlanian

Ranch Hand

Posts: 522

posted 12 years ago

Let me help you a little bit on the algorithm

1) Save the first element of the array in a variable.

2) Loop through the elements of the array and assign each element to it predecessor.

3) Assign the value you saved in step 1 to the last element of the array.

1) Save the first element of the array in a variable.

2) Loop through the elements of the array and assign each element to it predecessor.

3) Assign the value you saved in step 1 to the last element of the array.

- Do not try and bend the spoon. That's impossible. Instead, only try to realize the truth. - What truth? - That there is no spoon!!!

Ken Ng

Greenhorn

Posts: 26

Vicken Karaoghlanian

Ranch Hand

Posts: 522

Michael Dunn

Ranch Hand

Posts: 4632

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 12 years ago

Say you have 1-2-3-4-5 written around a wheel like this:

If you rotate two positions counterclockwise, it will come out 3-4-5-1-2. I have to put my fingers on the wheel and turn click, click to make sure that's right.

right?

Where 1 used to be first 3 is now first. Hmmm, to rotate two positions, we skip two positions over to find the new first element. That has promise:

Fortunately that wrapping around the wheel is available in math with modulus, which gives the remainder of a division. So this will work for any counterclockwise rotation up to 4.

See if you can turn that into a more generic loop. The rotated index goes from 0 to length of array-1. The x index goes from 2 however much you rotate (this time 2) and wraps around.

I hope that made sense. Now look at clockwise.

This is just the same except we subtract the number of rotations from zero. So x starts out at -2. We have to make this positive first, so add the length of the array: -2 + 5 = 3. Now we can start around the wheel exactly as before.

What we're doing here is commonly called a "circular buffer" - a fixed sized array that we use from beginning to end and then start over. This is common in some kinds of stream processing and may come in handy in your future.

Hey, how do you make it work for any rotation? Say somebody wants to rotate 99, like spinning the wheel of fortune on TV!

If you rotate two positions counterclockwise, it will come out 3-4-5-1-2. I have to put my fingers on the wheel and turn click, click to make sure that's right.

right?

Where 1 used to be first 3 is now first. Hmmm, to rotate two positions, we skip two positions over to find the new first element. That has promise:

Fortunately that wrapping around the wheel is available in math with modulus, which gives the remainder of a division. So this will work for any counterclockwise rotation up to 4.

See if you can turn that into a more generic loop. The rotated index goes from 0 to length of array-1. The x index goes from 2 however much you rotate (this time 2) and wraps around.

I hope that made sense. Now look at clockwise.

This is just the same except we subtract the number of rotations from zero. So x starts out at -2. We have to make this positive first, so add the length of the array: -2 + 5 = 3. Now we can start around the wheel exactly as before.

What we're doing here is commonly called a "circular buffer" - a fixed sized array that we use from beginning to end and then start over. This is common in some kinds of stream processing and may come in handy in your future.

Hey, how do you make it work for any rotation? Say somebody wants to rotate 99, like spinning the wheel of fortune on TV!

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi

It is sorta covered in the JavaRanch Style Guide. |