Rotate Arrays in Java (original) (raw)

In Java, array rotation refers to shifting the elements of an array by a specified number of positions. This operation can be performed in either direction: clockwise (right rotation) or counter-clockwise (left rotation). Array rotation finds applications in various domains such as algorithm design, data manipulation, and cryptography. This article will explore different approaches to performing array rotation in Java.

1. Example of Array Rotation

To illustrate an example of array rotation, let’s manually perform an array rotation on the array [1, 2, 3, 4, 5] by shifting the elements to the left by 2 positions.

Initial Array:

[1, 2, 3, 4, 5]

Step 1: Shifting elements individually

We’ll start by moving each element one position to the left.

Move 1 to the end:

[2, 3, 4, 5, 1]

Move 2 to the end:

[3, 4, 5, 1, 2]

Step 2: Final Rotated Array

After shifting each element by 1 position for 2 times, we get the final rotated array:

[3, 4, 5, 1, 2]

Summary of illustration:

Initial: [1, 2, 3, 4, 5] Step 1: [2, 3, 4, 5, 1] Step 2: [3, 4, 5, 1, 2] (Final)

This example shows a manual illustration of array rotation by shifting each element individually.

2. Brute Force Approach

The simplest method for array rotation involves shifting each element of the array one position to the left or right, depending on the direction of rotation. While this approach is straightforward, it may not be the most efficient for large arrays, as it requires shifting elements individually.

2.1 Example

public class ArrayRotation {

public static void rotateLeft(int[] arr, int d) {
    int n = arr.length;
    for (int i = 0; i < d; i++) {
        int temp = arr[0];
        for (int j = 0; j < n - 1; j++) {
            arr[j] = arr[j + 1];
        }
        arr[n - 1] = temp;
    }
}

public static void main(String[] args) {
    int[] array = {1, 2, 3, 4, 5};
    int rotations = 2;
    rotateLeft(array, rotations);
    for (int num : array) {
        System.out.print(num + " ");
    }
}

}

2.2 Explanation

This approach is the simplest method for array rotation, where each array element is shifted one position at a time.

2.2.1 Time Complexity

2.2.2 Space Complexity

2.2.3 Output

After rotating the array left by 2 positions using the Brute Force Approach, the output is [3, 4, 5, 1, 2].

Fig 1: Output - Java Arrays Rotation using Brute Force Approach

Fig 1: Output – Java Arrays Rotation using Brute Force Approach

3. Using Temporary Array

Another approach involves using an auxiliary array to store the rotated elements. This method is simple to implement and has a time complexity of O(n), where n is the number of elements in the array.

3.1 Example

public class RotateWithTemporaryArray {

public static void rotateLeft(int[] arr, int d) {
    int n = arr.length;
    int[] temp = new int[d];
    for (int i = 0; i < d; i++) {
        temp[i] = arr[i];
    }
    for (int i = d; i < n; i++) {
        arr[i - d] = arr[i];
    }
    for (int i = 0; i < d; i++) {
        arr[n - d + i] = temp[i];
    }
}

public static void main(String[] args) {
    int[] array = {2, 4, 6, 8, 10};
    int rotations = 3;
    rotateLeft(array, rotations);
    for (int num : array) {
        System.out.print(num + " ");
    }
}

}

3.2 Explanation

In this method, we use an auxiliary array to store the rotated elements.

3.2.1 Time Complexity

3.2.2 Space Complexity

3.2.3 Output Explanation

8 10 2 4 6

4. Reversal Algorithm

The reversal algorithm is a more efficient approach for array rotation. It involves reversing the elements of the array in segments and then reversing the entire array.

4.1 Example

public class RotateArrayWithReverseAlgorithm {

public static void reverse(int[] arr, int start, int end) {
    while (start < end) {
        int temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}

public static void rotateLeft(int[] arr, int d) {
    int n = arr.length;
    reverse(arr, 0, d - 1);
    reverse(arr, d, n - 1);
    reverse(arr, 0, n - 1);
}

public static void main(String[] args) {
    
    int[] array = {3, 6, 9, 12, 15, 18};
    int rotations = 3;
    rotateLeft(array, rotations);
    for (int num : array) {
        System.out.print(num + " ");
    }
}

}

4.2 Explanation

The reversal algorithm involves reversing segments of the array and then reversing the entire array.

4.2.1 Time Complexity

4.2.2 Space Complexity

4.2.3 Output Explanation

12 15 18 3 6 9

5. Conclusion

In conclusion, while all three methods explored in this article achieve array rotation, the reversal algorithm and using a temporary array offer better time complexity than the brute force approach. However, the space complexity of using a temporary array is higher compared to the other two methods.

Finally, the choice of method depends on factors such as the size of the array, the number of rotations required, and the trade-offs between time and space complexities. By understanding and implementing these algorithms, we can efficiently rotate arrays in our Java programs.

6. Download the Source Code

This was an article on how to Rotate Arrays in Java.

Download
You can download the full source code of this example here: Rotate Arrays in Java

Photo of Omozegie Aziegbe

Omos holds a Master degree in Information Engineering with Network Management from the Robert Gordon University, Aberdeen. Omos is currently a freelance web/application developer who is currently focused on developing Java enterprise applications with the Jakarta EE framework.