Saturday, 12 December 2015

Program for array rotation


Program for array rotation


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

Array
Rotation of the above array by 2 will make array
ArrayRotation1
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
endTime 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)

ArrayRotation

          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}

Java Code for Juggler Algorithm :

package com.examples.ajay;


public class RotateArrrayJugglerWay {

 private static int findGCD(int n, int d){
  if(n % d == 0) return d;
  else return findGCD(d, n % d);
 }
 private static void leftRotate(int[] array, int dist, int length) {
  // TODO Auto-generated method stub
  int gcd = findGCD(length, dist);
  int set_number = length / gcd;
  for(int i =0; i< gcd; i++){
   int temp = array[i];
   int j = i;
   while((j+gcd) < length){
    array[j] = array[j + gcd];
    j = j + gcd;
   }
   array[j] = temp;
  }
  for (int element : array) {
   System.out.print(element + " ");
  }
 }
 public static void main(String[] args) {
  int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
  leftRotate(array, 11, array.length);
 }
}
Time complexity: O(n)
Auxiliary Space: O(1)
Method 4(The Reversal Algorithm) Please read this for first three methods of array rotation.
Algorithm:
rotate(arr[], d, n)
  reverse(arr[], 1, d) ;
  reverse(arr[], d + 1, n);
  reverse(arr[], l, n);
Let AB are the two parts of the input array where A = arr[0..d-1] and B = arr[d..n-1]. The idea of the algorithm is: Reverse A to get ArB. /* Ar is reverse of A */ Reverse B to get ArBr. /* Br is reverse of B */ Reverse all to get (ArBr) r = BA.
For arr[] = [1, 2, 3, 4, 5, 6, 7], d =2 and n = 7 A = [1, 2] and B = [3, 4, 5, 6, 7] Reverse A, we get ArB = [2, 1, 3, 4, 5, 6, 7] Reverse B, we get ArBr = [2, 1, 7, 6, 5, 4, 3] Reverse all, we get (ArBr)r = [3, 4, 5, 6, 7, 1, 2]
Note : Examples and description have been taken from geeksforgeeks.org.

1 comment:

  1. copied from https://www.geeksforgeeks.org/array-rotation/

    ReplyDelete