Java program for rotation of elements of array- left and right

Rotation of element of array – left and right

In this program we’ll be learning about Java program for rotation of elements of array- left and right to a specified number of times. An array is said to be right rotated if all the selected elements were  moved towards right by one position. The last element of array will become the first element of array after rotation and vice versa for left rotation.

Rotation of elements of array- left and right in java

Keypoint of Java program for rotation of elements of array- left and right

In this section we will learn about basic knowledge which we need to know before coding the above Program. So we must have knowledge of what is an array? 

What is an array?
An array is a data structure, it is collection of similar data elements which is stored at contiguous memory locations in which each data element can be accessed directly by only using its index number.
 
How to declare an array?
To declare an array in C,a programmer specifies the type of the elements and the number of elements required by an array as follows − This is called a single-dimensional array. The arraySize must be an integer constant greater than zero and type can be any valid C data type. For example, to declare a 10-element array called balanceof type double, use this statement − Here balanceis a variable array which is sufficient to hold up to 10 double numbers.
 
About Java language:-
Java is class-based, object-oriented programming language and used for Internet-based applications. Java is a high-level
language.

Seperate codes for Java program for rotation of elements of array- left and right

Left Rotation

          Here we will be learning about Left rotation in an Array

     Algorithm.

  • STEP 1: START
  • STEP 2: INITIALIZE arr[] ={10, 20, 30, 40, 50 }.
  • STEP 3: SET n =3
  • STEP 4: PRINT “Elements of Array”
  • STEP 5: REPEAT STEP 6 UNTIL i<arr.length
                //for(i=0; i<arr.length; i++)
  • STEP 6: PRINT arr[i]
  • STEP 7: REPEAT STEP 8 to STEP 12 UNTIL i<n
                // for(i=0; i<n; i++ )
  • STEP 8: DEFINE j, first.
  • STEP 9: first = arr[0]
  • STEP 10: REPEAT STEP 11 UNTIL j>0
                //for(j=0 ;j<arr.length-1; j++)
  • STEP 11: arr[j]= arr[j+1]
  • STEP 12: arr[j]= first
  • STEP 13: PRINT “Array after rotation”
  • STEP 14: REPEAT STEP 15 UNTIL i<arr.length
                //for(i=0; i<arr.length; i++)
  • STEP 15: PRINT arr[i]
  • STEP 16: END
Java Program for rotation left

Code For Java program for rotation of elements of array- left

import java.util.*;

class PrepInsta
      {
        public static void main(String[] args)
          {  
            //Initialize array  
            int [] arr = new int [] {10, 20, 30, 40, 50};  
            //n determine the number of times an array should be rotated  
            int n = 3;  
            //Displays elements of array  
            System.out.println("Original array: ");  
            for (int i = 0; i < arr.l; i++)
            {  
                System.out.print(arr[i] + " ");  
            }  
            //Rotate the given array by n times toward left  
            for(int i = 0; i < n; i++){  
                int j, first;  
                //Stores the first element of the array  
                first = arr[0];  
                for(j = 0; j < arr.l-1; j++)
                {  
                    //Shift element of array by one  
                    arr[j] = arr[j+1];  
                }  
                //First element of array will be added to the end  
                arr[j] = first;  
            }  
            System.out.println();  
            //Displays resulting elements of array after rotation  
            System.out.println("Array after left rotation: ");  
            for(int i = 0; i< arr.l; i++)
            {  
                System.out.print(arr[i] + " ");  
            }  
        }  
    }

Output:-

Original array: {10,20,30,40,50}

Array after left rotation: {30,40,50,10,20}

Right Rotation

Over here we will be learning how to rotate our array towards right.

Algorithm.

  • STEP 1: START
  • STEP 2: INITIALIZE arr[] ={10, 20, 30, 40, 50 }.
  • STEP 3: SET n =3
  • STEP 4: PRINT “Elements of Array”
  • STEP 5: REPEAT STEP 6 UNTIL i<arr.length
                //for(i=0; i<arr.length; i++)
  • STEP 6: PRINT arr[i]
  • STEP 7: REPEAT STEP 8 to STEP 12 UNTIL i<n
                // for(i=0; i<n; i++ )
  • STEP 8: DEFINE j, last.
  • STEP 9: last = arr[arr.length-1]
  • STEP 10: REPEAT STEP 11 UNTIL j>0
                //for(j= arr.length-1;j>0; j–)
  • STEP 11: arr[j]= arr[j-1]
  • STEP 12: arr[0]= last
  • STEP 13: PRINT “Array after rotation”
  • STEP 14: REPEAT STEP 15 UNTIL i<arr.length
                //for(i=0; i<arr.length; i++)
  • STEP 15: PRINT arr[i]
  • STEP 16: END
Java Program for rotation right

Code For Java program for rotation of elements of array- right

class PrepInsta; {    
     public static void main(String[] args) {    
           //Initialize array     
            int [] arr = new int [] {10, 20, 30, 40, 50};     
         //determine the number of times an array should be rotated.    
            int n = 3;    
               
            //Displays Elements of array    
            System.out.println("Elements of array: ");    
            for (int i = 0; i < arr.length; i++) {     
                System.out.print(arr[i] + " ");     
            }      
                
       //Rotate the given array by n times toward right    
            for(int i = 0; i < n; i++){ int j, last; //Stores the last element of array last = arr[arr.length-1]; 
                     for(j = arr.length-1; j > 0; j--){    
                  //Shift element of array by one    
                    arr[j] = arr[j-1];    
                }    
             //Last element of array will be added to the start of array.    
                arr[0] = last;    
            }    
            
            System.out.println();    
                
            //Displays resulting elements of array after rotation    
            System.out.println("Array after right rotation: ");    
            for(int i = 0; i< arr.length; i++){    
                System.out.print(arr[i]  " ");    
            }    
        }    
    }

Output:-

Element of array: {10,20,30,40,50}

Array after right rotation: {40,50,10,20,30}