将数组元素循环移动p位,交换次数仅为n次

算法思路

循环左移p位

数组序列长度为n,左移p位。

算法步骤
1.将数组元素下标为0 -- p-1元素进行逆置;
2.将数组元素下标为p -- n-1元素进行逆置;
3.将数组元素下标为0 -- n-1元素进行逆置;
image.png
代码如下:
package Algorithm;

public class RCR {
    
    private int[] arr; 
    private int l;
    private int p;
    
    public RCR(int[] arr,int l,int p) {
        this.arr = arr;
        this.l = l;
        this.p = p;
    }
    
    public void reverse(int l,int r){
        int temp;
        int i,j;
    
        for (i=l,j=r; i this.arr.length || this.p < 0){
            System.out.println("ERROR");
        }else{
            this.reverse(0, this.p-1);
            this.reverse(this.p, this.arr.length-1);
            this.reverse(0, this.arr.length-1);
        }
    }
    
    private void rcrRight() {
        if(this.p > this.arr.length || this.p < 0){
            System.out.println("ERROR");
        }else{
            this.reverse(0, this.arr.length-1);
            this.reverse(0, this.p-1);
            this.reverse(this.p, this.arr.length-1);
        }
    }
    
    public void print(){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(this.arr[i]+"  ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        int[] arr ={1,2,3,4,5,6,7,8,9};
        int[] arr1 ={1,2,3,4,5,6,7,8,9};
        RCR rcr = new RCR(arr,arr.length,3);
        RCR rcr1 = new RCR(arr1,arr1.length,3);
        rcr.rcrLeft();
        rcr.print();
        //rcr1.rcrRight();
        //rcr1.print();
    }
}

循环左移p位

数组序列长度为n,右移p位。

算法步骤
1.将数组元素下标为0 -- n-1元素进行逆置;
2.将数组元素下标为p -- n-1元素进行逆置;
3.将数组元素下标为0 -- p-1元素进行逆置;
image.png
代码如下:
package Algorithm;

public class RCR {
    
    private int[] arr; 
    private int l;
    private int p;
    
    public RCR(int[] arr,int l,int p) {
        this.arr = arr;
        this.l = l;
        this.p = p;
    }
    
    public void reverse(int l,int r){
        int temp;
        int i,j;
    
        for (i=l,j=r; i this.arr.length || this.p < 0){
            System.out.println("ERROR");
        }else{
            this.reverse(0, this.p-1);
            this.reverse(this.p, this.arr.length-1);
            this.reverse(0, this.arr.length-1);
        }
    }
    
    private void rcrRight() {
        if(this.p > this.arr.length || this.p < 0){
            System.out.println("ERROR");
        }else{
            this.reverse(0, this.arr.length-1);
            this.reverse(0, this.p-1);
            this.reverse(this.p, this.arr.length-1);
        }
    }
    
    public void print(){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(this.arr[i]+"  ");
        }
        System.out.println();
    }
    
    public static void main(String[] args) {
        int[] arr ={1,2,3,4,5,6,7,8,9};
        int[] arr1 ={1,2,3,4,5,6,7,8,9};
        RCR rcr = new RCR(arr,arr.length,3);
        RCR rcr1 = new RCR(arr1,arr1.length,3);
        //rcr.rcrLeft();
        //rcr.print();
        rcr1.rcrRight();
        rcr1.print();
    }
}

注意

以上算法通过逆置运算,将移位运算的交换次数降到做小,即数组的长度为完成移位所需的交换次数。数组下标从0开始。

你可能感兴趣的:(将数组元素循环移动p位,交换次数仅为n次)