dsa2 min read

Matrix Data Structure: 2D Array Traversal, Transpose, Rotation, and Spiral Traversal (2026)

Matrix Data Structure: 2D Array Traversal, Transpose, Rotation, and Spiral Traversal (2026)

Published:  |  Category: Dsa  |  Reading time: ~15 min
Matrix Data Structure: 2D Array Traversal, Transpose, Rotation, and Spiral Traversal (2026)

A matrix is a 2D array of elements arranged in rows and columns. Matrices are fundamental in computer graphics, scientific computing, and machine learning. Operations on matrices include traversal, transposition, rotation, and specialized patterns like spiral traversal.

This tutorial covers row-major and column-major traversal, matrix transpose (turning rows into columns), 90-degree rotation, spiral order traversal, and searching in a sorted matrix with C++, Java, and Python examples.

2D Array Traversal and Transpose

Row-major traversal uses nested loops — outer loop for rows, inner loop for columns. Column-major reverses this. Transposition produces a new matrix where A[i][j] becomes A[j][i]. For an N x M matrix, the transpose will be M x N. In-place transpose works only for square matrices.

Traversal order matters for cache performance. Row-major is faster in C++ because arrays are stored row-wise. Python's NumPy also defaults to row-major storage.

// C++ — transpose
for(int i=0; i

Rotate Matrix 90 Degrees

Rotating a matrix 90 degrees clockwise can be done in two steps: transpose the matrix, then reverse each row. For a counter-clockwise rotation, transpose then reverse each column. This approach is O(n^2) time and O(1) extra space for square matrices.

An alternative is to rotate layers one ring at a time. The outermost layer is rotated first, then the next inner layer, until the center is reached. Both methods produce identical results.

// C++ — rotate 90° clockwise
void rotate(vector>& mat) {
  int n = mat.size();
  for(int i=0; i

Spiral Traversal and Search in Sorted Matrix

Spiral traversal visits elements in a clockwise spiral: right across the top row, down the right column, left across the bottom row, up the left column, then inward. Four boundaries (top, bottom, left, right) are maintained and updated after each direction is traversed.

Searching in a row-wise and column-wise sorted matrix uses the staircase approach: start at the top-right corner, move left if the target is smaller, move down if the target is larger. This yields O(m + n) time.

// C++ — spiral traversal
void spiral(vector>& mat) {
  int t=0, b=mat.size()-1, l=0, r=mat[0].size()-1;
  while(t<=b && l<=r) {
    for(int i=l; i<=r; i++) cout << mat[t][i] << " "; t++;
    for(int i=t; i<=b; i++) cout << mat[i][r] << " "; r--;
    if(t<=b) { for(int i=r; i>=l; i--) cout << mat[b][i] << " "; b--; }
    if(l<=r) { for(int i=b; i>=t; i--) cout << mat[i][l] << " "; l++; }
  }
}

// Java — similar boundary approach

// Python — spiral
def spiral(mat):
    res = []
    while mat:
        res += mat.pop(0)
        if mat and mat[0]:
            for row in mat: res.append(row.pop())
        if mat: res += mat.pop()[::-1]
        if mat and mat[0]:
            for row in mat[::-1]: res.append(row.pop(0))
    return res

Frequently Asked Questions

How do you access an element in a 2D array?

Use row and column indices: matrix[row][col]. In memory, it is stored as a flat array with index = row * numCols + col (row-major).

What is the time complexity of matrix transposition?

O(m * n) where m is the number of rows and n is the number of columns, since every element is visited once.

Originally published on Ayodhyyya. Last updated June 1, 2026.