0% found this document useful (0 votes)
82 views6 pages

Array Rotator1

This document provides an algorithm for rotating arrays to the left or right by a specified number of positions. It discusses: 1) Rotating an array right by n positions can be done by rotating left by (length of array - n) positions. 2) Rotating an array n times will return it to the original starting position if n is a multiple of the array length. 3) The algorithm avoids "cycles" by rotating in two steps if the rotation amount would cause looping through the same positions. 4) The time complexity is O(2n) for best case and O(3n) for worst case, with O(2) space complexity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views6 pages

Array Rotator1

This document provides an algorithm for rotating arrays to the left or right by a specified number of positions. It discusses: 1) Rotating an array right by n positions can be done by rotating left by (length of array - n) positions. 2) Rotating an array n times will return it to the original starting position if n is a multiple of the array length. 3) The algorithm avoids "cycles" by rotating in two steps if the rotation amount would cause looping through the same positions. 4) The time complexity is O(2n) for best case and O(3n) for worst case, with O(2) space complexity.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Author : Manikandan G

Email : [email protected]

Blog : gmanikandan.wordpress.com

Mobile : +91-9944306848

public class ArrayRotator<T> {


/*

General things about rotating array of elements to the Right and the Left
// In this notes where ever you see (N and n_ have the below things in mind
// N = denotes the length of the array we handle
// n = denotes that much time we want to rotate the array
rotating right by one place:
rotating each elements of array by one place towards right,
here the last element will become the first one after rotating
rotating left by one place:
rotating each elements of array by one place towards left, here
the first element will become the last one after rotating.
Eg of Right rotating:
1

( the above array rotated right 1 time)

( the above array rotated right 1 time)

( the above array rotated right 1 time)

( the above array is rotated right 3 times when compared to its initial state)

( the above array rotated right 1 time)

( the above array rotated right 1 time)

( the above array is rotated right 5 times when compared to its very initial state)

Eg of Left rotating:
1

( the above array rotated left 1 time)

( the above array rotated left 1 time)

( the above array is rotated left 2 times when compared to its very initial state)

Important rules of left rotating and right rotating


1) rotating array ( towards left or right ) by N times will make the

array to its very initial state (the state where no rotating is done)
In the above example of left rotating note that, after rotating 5 times towards
left we got our Array back as it was in the very initial state. By the using above
rule we can write like this.

rotating n times = rotating (n mod N) times


2) rotating right by n times

rotating left by (N - n) times.

In the above example note that


rotating array 3 times toward right = rotating array (5-3) time towards left
(see the highlighted rows)

General Ways by how we do rotating.


1) we may write algorithm to rotate array by one place (either right or left). We
call it repeatedly n times to do n places rotating.
The above way costs time complexity of O(n^2) and space complexity of O(1).
2) we may use another array of same size of actual array. In which each element is
copied to its new position (position after rotating)
The above way costs time complexity of O(n) and space complexity of O(n).

3) The third way ( I founded this one)


Array rotateArrayToRight(Array, n) {
N = Arrays length
newPosition = 0;
temporaryVariable =

Array[ newPosition ];

Do the below steps for N times

//calculate the newPosition of the element in temporaryVariable

newPosition = (newPosition

+ n) mod N;

anotherTemporaryVariable = Array[ newPosition ];


Array[ newPosition ] = temporaryVariable;
temporaryVariable = anotherTemporaryVariable ;

}
return array;
}
//end

wait friends before clapping for me >>


The above my algorithm works fine for the 'N' and 'n' that wont bring cycles /
loops while iteration.
Note that in our algorithm at every pass of iteration ,we supposed to find the
newPostions for the element stored in the temporaryVariable in our Array
we mean Cycle or loop in our business is..(I'll show with an example)

lets take N = 4, n = 2 and at the beginning newPosition will be 0


newPosition = ( newPosition + 2) mod 4
computing above statements for 4 times will give us
(0 + 2) mod 4 => 2
(2 + 2) mod 4 => 0
(0 + 2) mod 2 => 2
(2 + 2) mod 4 => 0

see we get only 0 and 2 as newPositions before we finish 4 iteration and this is
the state we mean as Cycle...

wait friends before scolding for me >>


we can eliminate this cycling behaviour, by calling the above algorithm twice
First Time with n-1 as n
Second Time with 1 as n
this will result same as by rotating n times.

Finally My Algorithm costs

*/

For Best Case

: 0(2n) of time complexity and O(2) of space complexity

For Worst Case

: 0(3n) of time complexity and O(2) of space complexity

// note only public methods exposed to others.


public T[] rotateArrayToLeft(T[] array, int position) {
// rotating n times = rotating (n mod N) times

position = position % array.length;


// rotating right by n times =

rotating left by (N - n) times.

int newPosition = array.length - position;


return rotateArrayToRight(array, newPosition);
}

public T[] rotateArrayToRight(T[] array, int position) {


// rotating n times = rotating (n mod N) times

int newPosition = position % array.length;


// If we dont want to rotate array just return it as it is

if (newPosition == 0) {
return array;
}// check is there any cycles and handle it specially
else if (newPosition!= 1
&&
isThereCycleWhileIteration(array.length, newPosition))
{
array = rotateArray(array, newPosition - 1);
return rotateArray(array, 1);
}// else handle it normally
else {
return rotateArray(array, newPosition);
}
}
// the below method returns true if firstPosition equals any other
// nextPosition before we finish iteration
private boolean isThereCycleWhileIteration(int arrayLenght, int position) {
int firstPosition = (0 + position) % arrayLenght;
int nextPostion = firstPosition;
for (int i = 1; i < arrayLenght; i++) {
nextPostion = (nextPostion + position) % arrayLenght;
if (nextPostion == firstPosition) {
return true;
}
}
return false;
}

// the method which implements my algorithm and it does rotating towards right
private T[] rotateArray(T[] array, int position) {
int newPosition = 0;
T temporaryVariable = array[newPosition];
T anotherTemporayVariable;
for (int count = 0; count < array.length; count++) {
newPosition = (newPosition + position) % (array.length);
anotherTemporayVariable = array[newPosition];
array[newPosition] = temporaryVariable;
temporaryVariable = anotherTemporayVariable;
}
return array;
}
}

You might also like