Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Ken Ng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you thought to try so far? What do you think might be some of the steps of the algorithm?
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ken Ng
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 522
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ken-
I modified your code, to make it a little bit more efficient.

[ March 15, 2004: Message edited by: Vicken Karaoghlanian ]
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another way, using JOptionPane
(needs error handling)
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic