📜  如何在Java向左或向右旋转数组

📅  最后修改于: 2021-09-06 05:41:07             🧑  作者: Mango

给定一个大小为ND索引的数组arr[] ,任务是将数组旋转D索引。

左旋转:数组从左旋转 D 元素

例子:

1.使用临时数组
方法:在此方法中,只需创建一个临时数组并将数组 arr[] 的元素从 0 复制到第 D 个索引。在移动之后,数组 arr[] 的其余元素从索引 D 到 N。然后将临时数组元素移动到原始数组。

下面是上述方法的实现:

Java
// Java program to left rotate
// an array by D elements
 
class GFG {
 
    // Function to left rotate arr[]
    // of size N by D
    void leftRotate(int arr[], int d, int n)
    {
        // create temp array of size d
        int temp[] = new int[d];
 
        // copy first d element in array temp
        for (int i = 0; i < d; i++)
            temp[i] = arr[i];
 
        // move the rest element to index
        // zero to N-d
        for (int i = d; i < n; i++) {
            arr[i - d] = arr[i];
        }
 
        // copy the temp array element
        // in origninal array
        for (int i = 0; i < d; i++) {
            arr[i + n - d] = temp[i];
        }
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}


Java
// Java program to left rotate
// an array by d elements
 
class GFG {
 
    // Function to left rotate arr[]
    // of size n by d
    void leftRotate(int arr[],
                    int d, int n)
    {
        for (int i = 0; i < d; i++)
            leftRotatebyOne(arr, n);
    }
 
    void leftRotatebyOne(int arr[], int n)
    {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[i] = temp;
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}


Java
// Java program to left rotate
// an array by d elements
 
class GFG {
 
    // Function to left rotate arr[]
    // of siz N by D
    void leftRotate(int arr[], int d,
                    int n)
    {
        // To handle if d >= n
        d = d % n;
        int i, j, k, temp;
        int g_c_d = gcd(d, n);
 
        for (i = 0; i < g_c_d; i++) {
 
            // move i-th values of blocks
            temp = arr[i];
            j = i;
            while (true) {
                k = j + d;
                if (k >= n)
                    k = k - n;
                if (k == i)
                    break;
                arr[j] = arr[k];
                j = k;
            }
            arr[j] = temp;
        }
    }
 
    // function to print an array
    void printArray(int arr[],
                    int size)
    {
        int i;
        for (i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Function to get gcd of a and b
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}


Java
// Java program to rotate an array by
// D elements
 
class GFG {
    // Function to right rotate arr[]
    // of size N by D
 
    void rightRotate(int arr[], int d, int n)
    {
          // if arr is rotated n times then
          // you get the same array
          while(d > n){
            d = d - n;
        }
       
        // create temp array of size d
        int temp[] = new int[n - d];
 
        // copy first N-D element in array temp
        for (int i = 0; i < n - d; i++)
            temp[i] = arr[i];
 
        // move the rest element to index
        // zero to D
        for (int i = n - d; i < n; i++) {
            arr[i - d - 1] = arr[i];
        }
 
        // copy the temp array element
        // in origninal array
        for (int i = 0; i < n - d; i++) {
            arr[i + d] = temp[i];
        }
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}


Java
// Java program to rotate an array by
// d elements
 
class GFG {
 
    // Function to right rotate arr[]
    // of size n by d
    void rightRotate(int arr[],
                     int d, int n)
    {
        for (int i = n; i > d; i--)
            rightRotatebyOne(arr, n);
    }
 
    void rightRotatebyOne(int arr[], int n)
    {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[i] = temp;
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}


Java
// Java program to rotate an array by
// d elements
 
class GFG {
 
    // Function to right rotate arr[]
    // of siz N by D
    void rightRotate(int arr[], int d, int n)
    {
 
        // to neglect n rotations if rtotations is greater
        // than size of arr
        while (d > n) {
            d = d - n;
        }
        // to use as left rotation
        d = n - d;
        d = d % n;
        int i, j, k, temp;
        int g_c_d = gcd(d, n);
        for (i = 0; i < g_c_d; i++) {
 
            // move i-th values of blocks
            temp = arr[i];
            j = i;
            while (true) {
                k = j + d;
                if (k >= n)
                    k = k - n;
                if (k == i)
                    break;
                arr[j] = arr[k];
                j = k;
            }
            arr[j] = temp;
        }
    }
 
    // UTILITY FUNCTIONS
 
    // function to print an array
    void printArray(int arr[], int size)
    {
        int i;
        for (i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Function to get gcd of a and b
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}


输出:
3 4 5 1 2

时间复杂度: O(N)
辅助空间: O(D)

2.一一旋转:
方法:递归地将数组一个一个元素旋转——

要旋转一,将 arr[0] 存储在临时变量 temp 中,将 arr[1] 移动到 arr[0],将 arr[2] 移动到 arr[1] …最后将 temp 移动到 arr[n-1]

下面是上述方法的实现:

Java

// Java program to left rotate
// an array by d elements
 
class GFG {
 
    // Function to left rotate arr[]
    // of size n by d
    void leftRotate(int arr[],
                    int d, int n)
    {
        for (int i = 0; i < d; i++)
            leftRotatebyOne(arr, n);
    }
 
    void leftRotatebyOne(int arr[], int n)
    {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[i] = temp;
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}
输出:
3 4 5 1 2

时间复杂度: O(N * D)
辅助空间: O(1)

3.杂耍算法:
方法:这是方法2的扩展,不是一个一个移动,而是将数组分成不同的集合,其中集合的数量等于n和d的GCD,并移动集合内的元素。

下面是上述方法的实现:

Java

// Java program to left rotate
// an array by d elements
 
class GFG {
 
    // Function to left rotate arr[]
    // of siz N by D
    void leftRotate(int arr[], int d,
                    int n)
    {
        // To handle if d >= n
        d = d % n;
        int i, j, k, temp;
        int g_c_d = gcd(d, n);
 
        for (i = 0; i < g_c_d; i++) {
 
            // move i-th values of blocks
            temp = arr[i];
            j = i;
            while (true) {
                k = j + d;
                if (k >= n)
                    k = k - n;
                if (k == i)
                    break;
                arr[j] = arr[k];
                j = k;
            }
            arr[j] = temp;
        }
    }
 
    // function to print an array
    void printArray(int arr[],
                    int size)
    {
        int i;
        for (i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Function to get gcd of a and b
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.leftRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}
输出:
3 4 5 1 2

时间复杂度: O(n)
辅助空间: O(1)

右旋转:数组从右旋转 D 元素

例子:

1.使用临时数组
方法:在这种方法中,只需创建一个临时数组,并将数组 arr[] 的元素从 0 复制到N – D索引。移动之后,数组 arr[] 的其余元素从索引 D 到 N。然后将临时数组元素移动到原始数组。

Input arr[] = [1, 2, 3, 4, 5], D = 2
1) Store the first d elements in a temp array
   temp[] = [1, 2, 3]
2) Shift rest of the arr[]
   arr[] = [4, 5]
3) Store back the D elements
   arr[] = [4, 5, 1, 2, 3]

下面是上述方法的实现:

Java

// Java program to rotate an array by
// D elements
 
class GFG {
    // Function to right rotate arr[]
    // of size N by D
 
    void rightRotate(int arr[], int d, int n)
    {
          // if arr is rotated n times then
          // you get the same array
          while(d > n){
            d = d - n;
        }
       
        // create temp array of size d
        int temp[] = new int[n - d];
 
        // copy first N-D element in array temp
        for (int i = 0; i < n - d; i++)
            temp[i] = arr[i];
 
        // move the rest element to index
        // zero to D
        for (int i = n - d; i < n; i++) {
            arr[i - d - 1] = arr[i];
        }
 
        // copy the temp array element
        // in origninal array
        for (int i = 0; i < n - d; i++) {
            arr[i + d] = temp[i];
        }
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}
输出:
4 5 1 2 3

时间复杂度: O(N)
辅助空间: O(D)

2.一一旋转:
方法:递归地将数组一个一个元素旋转——

要旋转 1,将 arr[N] 存储在临时变量 temp 中,将 arr[N-1] 移动到 arr[N],将 arr[N-2] 移动到 arr[N-1] …最后将 temp 移动到 arr[1] ]

下面是上述方法的实现:

Java

// Java program to rotate an array by
// d elements
 
class GFG {
 
    // Function to right rotate arr[]
    // of size n by d
    void rightRotate(int arr[],
                     int d, int n)
    {
        for (int i = n; i > d; i--)
            rightRotatebyOne(arr, n);
    }
 
    void rightRotatebyOne(int arr[], int n)
    {
        int i, temp;
        temp = arr[0];
        for (i = 0; i < n - 1; i++)
            arr[i] = arr[i + 1];
        arr[i] = temp;
    }
 
    // utility function to print an array
    void printArray(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}
输出:
4 5 1 2 3

时间复杂度: O(N * D)
辅助空间: O(1)

1.杂耍算法:
方法:这是方法2的扩展,不是一个一个移动,而是将数组分成不同的集合,其中集合的数量等于n和d的GCD,并移动集合内的元素。

下面是上述方法的实现:

Java

// Java program to rotate an array by
// d elements
 
class GFG {
 
    // Function to right rotate arr[]
    // of siz N by D
    void rightRotate(int arr[], int d, int n)
    {
 
        // to neglect n rotations if rtotations is greater
        // than size of arr
        while (d > n) {
            d = d - n;
        }
        // to use as left rotation
        d = n - d;
        d = d % n;
        int i, j, k, temp;
        int g_c_d = gcd(d, n);
        for (i = 0; i < g_c_d; i++) {
 
            // move i-th values of blocks
            temp = arr[i];
            j = i;
            while (true) {
                k = j + d;
                if (k >= n)
                    k = k - n;
                if (k == i)
                    break;
                arr[j] = arr[k];
                j = k;
            }
            arr[j] = temp;
        }
    }
 
    // UTILITY FUNCTIONS
 
    // function to print an array
    void printArray(int arr[], int size)
    {
        int i;
        for (i = 0; i < size; i++)
            System.out.print(arr[i] + " ");
    }
 
    // Function to get gcd of a and b
    int gcd(int a, int b)
    {
        if (b == 0)
            return a;
        else
            return gcd(b, a % b);
    }
 
    // Driver program to test above functions
    public static void main(String[] args)
    {
        GFG rotate = new GFG();
        int arr[] = { 1, 2, 3, 4, 5 };
        rotate.rightRotate(arr, 2, arr.length);
        rotate.printArray(arr, arr.length);
    }
}
输出
4 5 1 2 3 

时间复杂度: O(n)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live