Program for array rotation
Write a function rotate(ar[], d, n) that rotates arr[] of size n by d elements.

Rotation of the above array by 2 will make array

METHOD 1 (Use temp array)
Input arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2, n =7 1) Store d elements in a temp array temp[] = [1, 2] 2) Shift rest of the arr[] arr[] = [3, 4, 5, 6, 7, 6, 7] 3) Store back the d elements arr[] = [3, 4, 5, 6, 7, 1, 2]
Time complexity O(n)
Auxiliary Space: O(d)
METHOD 2 (Rotate one by one)
leftRotate(arr[], d, n)
start
For i = 0 to i < d
Left rotate all elements of arr[] by one
end
To rotate by one, store arr[0] in a temporary variable temp, move arr[1] to arr[0], arr[2] to arr[1] …and finally temp to arr[n-1]
Let us take the same example arr[] = [1, 2, 3, 4, 5, 6, 7], d = 2
Rotate arr[] by one 2 times
We get [2, 3, 4, 5, 6, 7, 1] after first rotation and [ 3, 4, 5, 6, 7, 1, 2] after second rotation.
/*Function to left Rotate arr[] of size n by 1*/
void leftRotatebyOne(int arr[], int n);
/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
int i;
for (i = 0; i < d; i++)
leftRotatebyOne(arr, n);
}
void leftRotatebyOne(int arr[], int n)
{
int i, temp;
temp = arr[0];
for (i = 0; i < n-1; i++)
arr[i] = arr[i+1];
arr[i] = temp;
}
/* utility function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
}
/* Driver program to test above functions */
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
leftRotate(arr, 2, 7);
printArray(arr, 7);
getchar();
return 0;
}
Time complexity: O(n*d)
Auxiliary Space: O(1)
METHOD 3 (A Juggling Algorithm)
This is an extension of method 2. Instead of moving one by one, divide the array in different sets
where number of sets is equal to GCD of n and d and move the elements within sets.
If GCD is 1 as is for the above example array (n = 7 and d =2), then elements will be moved within one set only, we just start with temp = arr[0] and keep moving arr[I+d] to arr[I] and finally store temp at the right place.
Here is an example for n =12 and d = 3. GCD is 3 and
Let arr[] be {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}
a) Elements are first moved in first set – (See below diagram for this movement)
arr[] after this step --> {4 2 3 7 5 6 10 8 9 1 11 12}
b) Then in second set.
arr[] after this step --> {4 5 3 7 8 6 10 11 9 1 2 12}
c) Finally in third set.
arr[] after this step --> {4 5 6 7 8 9 10 11 12 1 2 3}
/* function to print an array */
void printArray(int arr[], int size);
/*Fuction to get gcd of a and b*/
int gcd(int a,int b);
/*Function to left rotate arr[] of siz n by d*/
void leftRotate(int arr[], int d, int n)
{
int i, j, k, temp;
for (i = 0; i < gcd(d, n); i++)
{
/* move i-th values of blocks */
temp = arr[i];
j = i;
while(1)
{
k = j + d;
if (k >= n)
k = k - n;
if (k == i)
break;
arr[j] = arr[k];
j = k;
}
arr[j] = temp;
}
}
/*UTILITY FUNCTIONS*/
/* function to print an array */
void printArray(int arr[], int size)
{
int i;
for(i = 0; i < size; i++)
printf("%d ", arr[i]);
}
/*Fuction to get gcd of a and b*/
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
/* Driver program to test above functions */
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7};
leftRotate(arr, 2, 7);
printArray(arr, 7);
getchar();
return 0;
}
Time complexity: O(n)
Auxiliary Space: O(1)
Please see following posts for other methods of array rotation:
Block swap algorithm for array rotation
Reversal algorithm for array rotation
References:
http://www.cs.bell-labs.com/cm/cs/pearls/s02b.pdf
Please write comments if you find any bug in above programs/algorithms.
You may also like following posts
Y so complex algorithm. It can be easily done by.
reverse 0 to d, reverse d to n , then again reverse 0 to n.
The reversal algorithm has been published as a separate post (See http://www.geeksforgeeks.org/archives/2838) . This post is to for one more method to do rotation.
There is one more method which i read somewhere.
pseudo code...
Step1:reverse entire array.
Step2:reverse the first 'd' elements.
Step3:reverse the last 'n-d' elements.
sorry...it was already posted...
I guess this should work.
i=0,count=0; // a,b,temp are variables (for intermediate // book-keeping) while(count!=n) { if(d>i) { b=n+i-d; if(count==0) a=arr[i]; temp=a; a=arr[b]; arr[b]=temp; i=b; } else { b=i-d; temp=arr[b]; arr[b]=a; a=temp; i=b; } count++; } for(j=0;j<n;j++) printf("%d ",arr[j]); printf("\n"); }Example:
Arr: 1,2,3
d=2
Iteration 1('1' will be moved to its final position,replacing '2' that will be stored in the variable 'a') : 1,1,3
Iteration 2('2' will be moved to its final position,replacing '3' that will be stored in the variable 'a') : 1,1,2
Iteration 3('3' will be moved to its final position,replacing 1st '1') : 3,1,2
Time complexity: O(n)
Auxiliary Space: O(1)
how to rotate right by juggling
k = j + d; if (k >= n) k = k - n;This particular section of code will become
k = j - d; if (k < 0) k = n + k; //Remember k is negativeWell, the better option is use of linked list
This can be done using the method in http://www.azillionmonkeys.com/qed/case8.html. Here's my code for the same:
Order : O(n)
Logic : follow loops/cycles until you're done
void leftrotate(vector<int>& v, size_t k) { assert(k > 0 && k < v.size()); size_t count = 0; int prev, cur; int start = 0; int i = 0; int j = 0; while(1) { i = start; prev = v[i]; if(count >= v.size()) break; while(1) { j = i - k; if(j < 0) j += v.size(); if(j == start) break; cur = v[j]; v[j] = prev; count++; prev = cur; i = j; } v[start] = prev; count++; start++; } }Well I guess this is very similar to the GCD method anyway.
i think in method three gcd will be called each time the loop will be executed so it must be pre-calculated and then used in the loop
int g=gcd(n,d)
for(int i=0; i<g; i++)
{
}
what's space complexity of method 1?
void reverse(char* data) { char ch; int i = 0, len = strlen(data); while (i < (len / 2)) { ch = data[i]; data[i] = data[len - i - 1]; data[len - i - 1] = ch; i++; } }// left rotate by [rlen] void lrotate(char* data, int rlen) { char ch; int i = 0, len = strlen(data); while (i < len - rlen) { ch = data[i]; data[i] = data[(i + rlen) % len]; data[(i + rlen) % len] = ch; i++; } }it is not working
How is the time complexity of the juggling algorithm is O(n)??
If d is a factor of n, it is O(n), but if it is not the time complexity remains O(n*d) like in the second method.
Because common complexity is O(n/d * d) = O(n).
yeah even i think so...admin plz pt some light here
I think so too. Can anybody clarify it?