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

9:20 PM 12 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!

12 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
satyam
AUTHOR
January 28, 2018 at 10:09 PM delete

Thanks Harsh for sharing

Reply
avatar
April 27, 2018 at 11:06 AM delete

really nyce ..interview quetions,topics,projects everything is nyce ..java training hyderabad

Reply
avatar
shalinipriya
AUTHOR
August 30, 2018 at 12:28 PM delete

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
Devops training in velachry
Devops training in OMR
Deops training in annanagar
Devops training in chennai
Devops training in marathahalli
Devops training in rajajinagar
Devops training in BTM Layout



Reply
avatar
amilu B
AUTHOR
September 8, 2018 at 12:30 PM delete

Thanks Admin for sharing such a useful post, I hope it’s useful to many individuals for developing their skill to get good career.

java training in annanagar | java training in chennai

java training in marathahalli | java training in btm layout

java training in rajaji nagar | java training in jayanagar

Reply
avatar
genga g
AUTHOR
September 13, 2018 at 1:19 PM delete This comment has been removed by the author.
avatar
Revathy A
AUTHOR
September 21, 2018 at 4:27 PM delete



It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...

angularjs-Training in velachery

angularjs-Training in annanagar

angularjs Training in chennai

angularjs Training in chennai

Reply
avatar
sai
AUTHOR
October 4, 2018 at 4:02 PM delete

Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
python online training
python training course in chennai
python training in jayanagar

Reply
avatar
sai
AUTHOR
October 4, 2018 at 4:04 PM delete

I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
python online training
python training course in chennai
python training in jayanagar

Reply
avatar
Unknown
AUTHOR
October 6, 2018 at 10:14 AM delete

myTectra Placement Portal is a Web based portal brings Potentials Employers and myTectra Candidates on a common platform for placement assistance

Reply
avatar
October 8, 2018 at 12:46 PM delete

Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
Devops Training in pune

Reply
avatar