首頁 > Java > java教程 > 主體

Java程式用於數組的右旋轉的逆序演算法

王林
發布: 2023-08-28 22:05:05
轉載
793 人瀏覽過

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.ed can can't change size i.e.

本文將幫助您了解反轉演算法,並且我們將建立一個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學習者快速成長!