Rotating a Matrix by 90 Degrees: A Java Implementation

Rotating a matrix by 90 degrees is a common problem encountered in image processing, game development, and other areas of computer science. In this blog post, we'll explore a Java implementation that efficiently rotates a square matrix by 90 degrees clockwise.

Understanding the Problem

Given a square matrix, we need to transform it such that its rows become columns and vice-versa, effectively rotating the matrix clockwise by 90 degrees.

The Algorithm: Transpose and Reverse

Our approach involves two key steps:

  1. Transpose the Matrix:

    • Transpose means swapping the rows and columns. In other words, element matrix[i][j] becomes matrix[j][i].
  2. Reverse Each Row:

    • After transposing, we reverse each row of the matrix. This completes the 90-degree clockwise rotation.

Java Code Implementation

Java

package Arrays;

public class RotateMatrixby90Degress {

    public static void rotate(int num[][]) {
        int row = num.length;
        int col = num[0].length;

        System.out.println("Original Array");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(num[i][j] + " ");
            }
            System.out.println();
        }

        // Transpose the matrix
        for (int i = 0; i < row; i++) {
            for (int j = i; j < col; j++) {
                int temp = num[i][j];
                num[i][j] = num[j][i];
                num[j][i] = temp;
            }
        }

        System.out.println("Transposed Array");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(num[i][j] + " ");
            }
            System.out.println();
        }

        // Reverse each row
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col / 2; j++) {
                int temp = num[i][j];
                num[i][j] = num[i][col - j - 1];
                num[i][col - j - 1] = temp;
            }
        }

        System.out.println("Rotated 90 degrees Array");
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                System.out.print(num[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int num[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
        rotate(num);
    }
}

Explanation:

  1. rotate(int num[][]):

    • Takes a 2D integer array num as input.

    • Prints the original matrix.

    • Transpose: The nested loops swap num[i][j] and num[j][i] for j >= i, effectively transposing the matrix.

    • Prints the transposed matrix.

    • Reverse Rows: The inner loop reverses each row by swapping elements from the beginning and end of the row until the middle is reached.

    • Prints the rotated matrix.

  2. main(String[] args):

    • Creates a sample 3x3 matrix.

    • Calls the rotate method to perform the rotation.

Example

For the input matrix:

1 2 3
4 5 6
7 8 9

The output will be:

Original Array
1 2 3 
4 5 6 
7 8 9 
Transposed Array
1 4 7 
2 5 8 
3 6 9 
Rotated 90 degrees Array
7 4 1 
8 5 2 
9 6 3

Time and Space Complexity

  • Time Complexity: O(n^2), where n is the number of rows (or columns) in the matrix, as we iterate through all elements of the matrix multiple times.

  • Space Complexity: O(1), as we perform the rotation in place without using any additional data structures of significant size.

Applications

Rotating matrices is a fundamental operation in:

  • Image Processing: Rotating images by multiples of 90 degrees.

  • Game Development: Rotating game elements or levels.

  • Cryptography: Some encryption algorithms involve matrix rotations.

  • Data Analysis: Transforming data representations.

This Java implementation provides a clear and efficient way to rotate a square matrix by 90 degrees. Understanding this technique can be beneficial in various programming scenarios.