Circular Shifting of elements in Arrays - Java program to shift array elements circularly

9:20 PM 1 Comments A+ a-

What do we mean by shifting the elements of an array

Many a times in our programs, we need to shift the elements of our array either to the left or to the right. For example, while dynamically adding any new element in between the existing elements of an array, we will be right shifting the elements after the inserted position. Similarly, we will do a left shift of the elements when we remove an element.

Circular Shifting in Arrays  - Right Circular Shift and Left Circular Shift

As we are now familiar with what we mean by shifting the array elements, the term circular adds a little more complexity to the otherwise a simple process of shifting elements of arrays to the left or right. 

Left Circular Shifting of Array Elements: In left circular shifting of array elements by N positions, we will take the first N elements starting from the left most side of the array and start placing them from the rear of the array in the same order as we took out them from the front of array. 
for example: 
Suppose we have an input array like this:
10, 20, 30, 40, 50
After left circular shift by 2 positions, the array would become:-
30, 40, 50, 10, 20

Right Circular Shifting of Array Elements: The Right circular shifting of arrays is exactly opposite to that of the Left circular shifting. In this we will be taking out the N elements from the rear of the array and will be inserting them to the front of the array. The below example would help us to understand this much easily.
for example:-
We have an input array: 10, 20, 30, 40, 50
After right circular shift by 2 positions: 40, 50, 10, 20, 30

Java program to implement left circular shifting and right circular shifting of array elements


public class CodeInJava{

 public static void main(String[] args) {
  CodeInJava obj = new CodeInJava();
  //input array
  int[] inputArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
  
  obj.printArray( inputArray );
  
  //circular left shifted array by 2 positions. 
  int[] leftShiftedArray = obj.circularShiftSingle( inputArray, 2, false );
  
  obj.printArray(leftShiftedArray);
  
  //circular right shifted array by 2 positions
  int[] rightShiftedArray = obj.circularShiftSingle( inputArray, 2, true );
  
  obj.printArray( rightShiftedArray );
  
 }
 
 public int[] circularShiftSingle(int[] array, int shift, boolean right) {
  int[] array2 = new int[shift];
  if(right) {
   for (int i = 0;i < shift;i++)
    array2[i] = array[array.length - shift + i];
   System.arraycopy(array, 0, array, shift, array.length - shift);
   for (int i = 0;i < shift;i++) {
    array[i] = array2[i];
   }
  }
  else {
   for (int i = 0;i < shift;i++)
    array2[i] = array[i];
   System.arraycopy(array, shift, array, 0, array.length - shift);
   for (int i = array.length - shift; i < array.length; i++) {
    array[i] = array2[shift + i - array.length];
   }
  }
  return array;
 }
 
 //utility method just to display the elements of an array
 private void printArray(int[] array) {
  for (int element:array)
   System.out.print(element + ", ");
  System.out.println();
 }

}

Output:

10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 
30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 
10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 
The first line shows the input as it is.
The second line shows the input after circular left shifting by 2 positions.
The third line shows the array in 2nd line after circular right shifting by 2 positions.
So, we shifted 2 positions left and then we shift 2 positions right on the same input array and we will get the same input array back in line 3rd. 

Currently working at Symantec in Chennai. Previously worked as a Senior Software Engineer at IGATE. Loves to explore new things, loves to code and solve challenging problems with the help of it. Like any other normal person, loves to watch movies, listen to songs (specially when travelling, it is must), getting to know people, travelling and whatever makes me feel good!

1 comments:

Write comments
oracle fusion
AUTHOR
March 28, 2017 at 2:57 PM delete

I would like to thank you for the efforts you have made in writing this blog.I am hoping the same best work from you in the future as well. Your write up is a fine example of it.For more details about oracle fusion financial please check our website.


Oracle Fusion Financial Training Institute

Reply
avatar