DSA Visualizer
GRID

Row-Major Traversal

The order you read a page in: across the first row, then the next. Press play and watch i and j move.

Left to right, one row at a time. The order most languages store a 2D array in memory.

0
1
2
3
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
Visited
Press play to start
Row-major order reads the grid the way you read a page: left to right, one row at a time, top to bottom.
0
VisitedVisited
12
LeftRemaining
12
TotalTotal
1 / 17

Pseudocode

Row-major
1for i in 0 .. rows-1:
2 for j in 0 .. cols-1:
3 visit(a[i][j])

Legend

Visiting nowVisited

Grid size

Want your own values? Edit this grid in the 2D Array Visualizer →

Tip: Space play/pause · ← → step

Learn row-major traversal

Row-major traversal reads the grid the way you read a page: across the first row, then across the second, and so on. It is the order most languages store a 2D array in memory, so it is also the fastest way to touch every cell.

A traversal is just a rule for the order in which you visit cells. The grid does not change; only the path through it does. Compare it with column-major, snake, diagonal, boundary and spiral.

Every full traversal costs one step per cell, rows × cols, which is written O(rows · cols). Boundary is the exception: it visits only the outer ring.

How row-major traversal works

Read this paragraph. Your eyes go left to right along a line, jump back to the start of the next line, and carry on. That is row-major order. The outer loop picks a row, the inner loop walks across it, and when the row runs out you drop down to the next one.

It is the first traversal everyone learns, and it deserves to be, because it matches how most programming languages store a 2D array: row 0's values sit next to each other in memory, then row 1's, then row 2's. Visiting the grid in this order means visiting memory in a straight line.

All it needs from the grid is a rectangle: every row the same length. In the visualizer, i is the current row and j the current column, and the row and column headers light up as they move.

The loop shape

Two nested loops. The outer one runs i from 0 to rows − 1; the inner one runs j from 0 to cols − 1. Whatever you want to do with a cell goes inside the inner loop, where both indices are known.

Notice which index changes fastest. j ticks on every step; i only changes when j has run out. That “inner index moves fastest” rule is the whole difference between row-major and column-major order.

A worked example: a 3 × 4 grid

Number the cells 1 to 12 the way they are written, and row-major order simply reads them back in the same order:

1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12

Row 0 gives 4 steps, row 1 gives 4 more, row 2 the last 4. Twelve cells, twelve steps, and the Visited counter above ends on 12. Press play on the visualizer with the default grid to see exactly this.

Where you'll meet it

  • Printing a grid. One row per line is row-major order with a newline after the inner loop.
  • Summing, searching, counting. Any “look at every cell” job defaults to this order.
  • Images. A photo is a grid of pixels stored row by row; image filters walk it in this order.
  • Flattening. The cell [i][j] lands at position i × cols + j in a 1D array. The visit number on each cell above is exactly that position plus one.

Mistakes beginners make

  • Mixing up the indices. a[i][j] is row i, column j. Writing a[j][i] silently transposes the grid, or crashes when it is not square.
  • Using the wrong length. The outer loop goes to a.length (rows), the inner to a[i].length (columns). On a 3 × 4 grid the two are different.
  • Off by one. Indices start at 0, so the last column is cols − 1. A loop that runs to cols reads past the end.

Next, swap the loops and watch what changes: Column-major traversal →

Row-major questions

What does row-major order mean?

It means the cells of a 2D array are visited, or stored, one whole row at a time: all of row 0 from left to right, then all of row 1, and so on. The row index changes slowly and the column index changes quickly.

Which languages store 2D arrays in row-major order?

C, C++, Java, C#, JavaScript arrays of arrays, Python lists of lists and NumPy's default layout all keep each row's values next to each other in memory. Fortran, MATLAB, R and Julia use column-major order instead.

Why is row-major traversal faster than column-major on most data?

Because the values of a row sit next to each other in memory, and a processor fetches memory in chunks called cache lines. Walking along a row uses every value in each chunk. Walking down a column touches one value per chunk and throws the rest away, which can be several times slower on a large grid.

How many steps does a row-major traversal take?

One per cell: rows × columns. On a 3 × 4 grid that is 12 steps, and doubling both dimensions quadruples the work, which is written O(rows × cols).