📜  循环置换数组元素的Java程序

📅  最后修改于: 2022-05-13 01:55:43.199000             🧑  作者: Mango

循环置换数组元素的Java程序

给定一个整数数组,我们在那里循环置换其元素,即将每个数组元素向左移动一个索引。第一个值将进入最后一个索引。

例子:

Input:    [1,2,3,4,5]
Output: [2,3,4,5,1]

Input:     [2,3,1,5,6]
Output:    [3,1,5,6,2]

方法#1

  1. 在函数cyclicShift() 中,for(i=0; i
  2. 在循环之前,数组的第一个值存储在变量 x 中。
  3. 最后,数组的最后一个元素被设置为 x。
Java
// Java Program to Cyclically Permute 
// the Elements of an Array
import java.util.*;
import java.io.*;
// function to print the array
class cycle {
    public int[] cycleShift(int[] arr)
    {
        int x = arr[0]; // store a[0]
        int i;
        for (i = 0; i < arr.length - 1; i++) {
            
            // for other element shift left
            arr[i] = arr[i + 1];
        }
        // for the last element in the modified array
        // it will be starting element
        arr[i] = x;
        return arr;
    }
}
public class GFG {
  
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        cycle c = new cycle();
        int[] newArray = c.cycleShift(arr);
        for (int i = 0; i < newArray.length; i++) {
            System.out.print(newArray[i] + " ");
        }
    }
}


Java
// Java Program to Cyclically Permute
// the Elements of an Array
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int first = arr[0];
        int start = 0;
        
        // swaping each element with the first
        // element
        for (int i = 1; i < arr.length; i++) {
            arr[start++] = arr[i];
            arr[i] = first;
        }
        // Printing the element in the
        // array.......
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}


Java
// Java Program to Cyclically Permute
// the Elements of an Array
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // use of the queue to do 
        // cyclic shift in the array
        int[] arr = { 1, 2, 3, 4, 5 };
        Queue q = new LinkedList<>();
        int first = arr[0];
        int strt = 0;
        
        // adding each element into the queue
        for (int i = 1; i < arr.length; i++) {
            q.add(arr[i]);
        }
  
        while (!q.isEmpty()) {
            arr[strt++] = q.poll();
        }
        
        // Polling out the element from the 
        // Queue and inserting into the queue
        arr[arr.length - 1] = first;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}


输出
2 3 4 5 1 
  • 时间复杂度: O(n),其中 n 是数组中元素的数量。
  • 空间复杂度: O(1)

方法#2:使用交换

在函数cyclicSwap(arr) 中,for(int i = 0; i < arr.length; i++) 循环将第一个元素交换到数组中的下一个元素

  1. 在交换后的第一次迭代中,原始数组 [1, 2, 3, 4, 5] –> [2, 1, 3, 4, 5];
  2. 在交换 [2, 1, 3, 4, 5] –> [2, 3, 1, 4, 5] 后再次在第二次迭代中;
  3. 并且这个迭代一直持续到循环结束最终结果将是这样的 [2, 3, 4, 5, 1]

下面是上述方法的实现。

Java

// Java Program to Cyclically Permute
// the Elements of an Array
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        int[] arr = { 1, 2, 3, 4, 5 };
        int first = arr[0];
        int start = 0;
        
        // swaping each element with the first
        // element
        for (int i = 1; i < arr.length; i++) {
            arr[start++] = arr[i];
            arr[i] = first;
        }
        // Printing the element in the
        // array.......
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
输出
2 3 4 5 1 
  • 时间复杂度: O(n)
  • 空间复杂度: O(1)

方法#3:使用队列在数组中进行循环置换

首先,将所有元素插入从索引1到arr.length的队列中;

使队列出队并存储回数组,最后将第一个元素放在数组的最后一个索引处

Java

// Java Program to Cyclically Permute
// the Elements of an Array
import java.io.*;
import java.util.*;
class GFG {
    public static void main(String[] args)
    {
        // use of the queue to do 
        // cyclic shift in the array
        int[] arr = { 1, 2, 3, 4, 5 };
        Queue q = new LinkedList<>();
        int first = arr[0];
        int strt = 0;
        
        // adding each element into the queue
        for (int i = 1; i < arr.length; i++) {
            q.add(arr[i]);
        }
  
        while (!q.isEmpty()) {
            arr[strt++] = q.poll();
        }
        
        // Polling out the element from the 
        // Queue and inserting into the queue
        arr[arr.length - 1] = first;
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
输出
2 3 4 5 1 
  • 时间复杂度: O(n)
  • 空间复杂度: O(n)