Java程序用于数组的右旋转的逆序算法

王林
王林 转载
2023-08-28 22:05:05 528浏览

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.

本文将帮助您了解反转算法,并且我们将创建一个Java程序,在其中创建一个数组,并通过应用反转算法进行向右旋转。

数组的右旋转

让我们在数组的上下文中理解“右旋转”这个术语。

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程序用于数组的右旋转的逆序算法

Example 2

的中文翻译为:

示例2

Java程序用于数组的右旋转的逆序算法

在上面的例子中,当我们将数组旋转2次时,从第0个位置开始的元素会被移动到第2个位置及以后的位置,而最后2个元素则被填充到前两个位置。

当我们将数组旋转4次时,从第0个位置开始的元素会被移动到第4个位置及以后。

声明数组的语法

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

我们可以在我们的程序中使用上述任何语法。

逆转算法

The approach for reversal algorithm is as follows −

  • 步骤1 - 首先,我们将给定的数组从第一个索引反转到最后一个索引。

  • 第二步 - 继续向前,我们将给定的数组从第一个索引到 rt - 1 的位置进行反转,其中 rt 是所需旋转的次数。

  • 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 − 它展示了方法的额外功能或行为,例如静态和最终。

  • 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 − 它包含变量的名称,后面跟着数据类型。

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 

在上面的代码中,我们创建了一个名为‘Rotation’的类,在该类中我们定义了两个带参数的方法‘rev’和‘rotates’。方法‘rev’用于交换元素,‘rotates’用于应用逆序算法的逻辑。在main()方法中,我们创建了一个名为‘obj’的‘Rotation’类对象,并使用该对象调用了‘rotates’方法,并传入了两个参数。

Conclusion

在本文中,我们了解了什么是右旋转,并讨论了反转算法。我们使用反转算法编写了一个用于数组右旋转的Java程序。

以上就是Java程序用于数组的右旋转的逆序算法的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除