Home  >  Article  >  Java  >  Java program for reverse order algorithm for right rotation of array

Java program for reverse order algorithm for right rotation of array

王林
王林forward
2023-08-28 22:05:05772browse

Array is a linear data structure that is used to store group of elements with similar datatypes. It stores data in a sequential manner. Once we create an array we can't change its size i.e. it can store fixed number of elements.

This article will help you understand the inversion algorithm and we will create a Java program in which we create an array and perform right rotation by applying the inversion algorithm.

Right rotation of array

Let us understand the term "right rotation" in the context of an array.

In right rotation of an array, we simply shift the elements of the array to our right till the specified number of rotations.

Example 1

Java program for reverse order algorithm for right rotation of array The Chinese translation of

Example 2

is:

Example 2

Java program for reverse order algorithm for right rotation of array

In the above example, when we rotate the array 2 times, the elements starting from the 0th position will be moved to the 2nd position and beyond, and the last 2 elements will be filled in the first two location.

When we rotate the array 4 times, the elements starting from the 0th position will be moved to the 4th position and beyond.

Syntax for declaring arrays

Data_Type nameOfarray[]; 
// declaration
Or,
// declaration with size
Data_Type nameOfarray[] = new Data_Type[sizeofarray]; 

We can use any of the above syntax in our program.

Reversal Algorithm

The approach for reversal algorithm is as follows −

  • Step 1 - First, we reverse the given array from the first index to the last index.

  • Step 2 - Continuing forward, we reverse the given array from the first index to the position of rt - 1, where rt is the number of rotations required.

  • Step3 − In the last step, we will reverse the remaining array i.e. from rt to last index.

Note that for shifting the elements of array we will perform the swapping between them.

Program for right rotation using Reversal Algorithm

We will put our logic in user-defined method. Let’s discuss how we can create a user-defined method.

Syntax

accessSpecifier nonAccessModifier return_Type nameOfmethod(Parameters) {
   // your code will come here
}
  • accessSpecifier − It is used to set the accessibility of the method. It may be public, protected, default and private.

  • nonAccessModifier − It exhibits extra functionality or behavior of a method, such as static and final.

  • return_Type − The datatype a method is going to return. We use void keyword when method does not return anything.

  • nameOfmethod − Name of the method.

  • parameters − It contains the name of the variable followed by the data type.

The Chinese translation of

Example

is:

Example

public class Rotation {
   public void rev(int rot_arr[], int first, int last) {
      while(first < last) {
         int temp = rot_arr[first];
         rot_arr[first] = rot_arr[last];
         rot_arr[last] = temp;
         first++;
         last--;
      }
   }
   public int[] rotates(int rot_arr[], int rt) {
      rt = rt % rot_arr.length;
      rev(rot_arr, 0, rot_arr.length - 1);
      rev(rot_arr, 0, rt - 1);
      rev(rot_arr, rt, rot_arr.length - 1);
      return rot_arr;
   }
   public static void main(String[] args) {
      Rotation obj = new Rotation(); 
      int rot_arr[] = {5, 8, 2, 4, 7, 1};
      int rt = 4;
      System.out.print(" The given array is: ");
      for(int i = 0; i < rot_arr.length; i++) {
         System.out.print(rot_arr[i] + " ");
      }
      obj.rotates(rot_arr, rt);
      System.out.println();
      System.out.print(" The given array after right rotation is: ");
      for(int i = 0; i < rot_arr.length; i++) {
         System.out.print(rot_arr[i] + " ");
      }
   }
}

Output

 The given array is: 5 8 2 4 7 1 
 The given array after right rotation is: 2 4 7 1 5 8 

In the above code, we created a class named 'Rotation', in which we defined two methods with parameters 'rev' and 'rotates'. The method 'rev' is used to exchange elements and 'rotates' is used to apply the logic of the reverse order algorithm. In the main() method, we created a 'Rotation' class object named 'obj' and used the object to call the 'rotates' method, passing in two parameters.

Conclusion

In this article, we learned what right rotation is and discussed the inversion algorithm. We have written a Java program for right rotation of an array using the inversion algorithm.

The above is the detailed content of Java program for reverse order algorithm for right rotation of array. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete