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

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.

- 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

Joined: Feb 14, 2004
Posts: 26

posted

0

Thanks for your hint, Vicken But i have problem with ANTI-CLOCKWISE Here's my code

Results in 2nd col belongs to ANTI-CLOCKWISE and is wrong! ANTI-CLOCKWISE algorithm looks OK, but can't figure out what is wrong? [ March 15, 2004: Message edited by: Ken Ng ]

Vicken Karaoghlanian
Ranch Hand

Joined: Jul 21, 2003
Posts: 522

posted

0

Ken- I modified your code, to make it a little bit more efficient.

[ March 15, 2004: Message edited by: Vicken Karaoghlanian ]

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!

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

subject: (anti)clockwise rotation of array elements...HELP!