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:
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 positioni × cols + jin 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 rowi, columnj. Writinga[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 toa[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 tocolsreads 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).