首页 > Java > java教程 > 正文

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

王林
发布: 2023-08-28 22:05:05
转载
794 人浏览过

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.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!