The six orders side by side
A traversal is a rule for the order in which you visit cells. Every full traversal on this page costs one step per cell, rows × cols; what differs is the path, and therefore which problems each order makes easy.
Which order should you learn first?
Start with row-major: it is the plain pair of nested loops, and the order most languages store a grid in. Then column-major, to see how much changes when you only swap the loops. Snake teaches reversing a loop; diagonal teaches loop bounds from max and min. Finish with boundary and then spiral, the interview favourite that is really a boundary walk repeated inwards.
Each page keeps your grid when you switch, so try one grid in all six orders and compare the visit numbers.
Matrix traversal questions
What does it mean to traverse a matrix?
To visit its cells one after another in some fixed order, doing something with each one: printing it, adding it up, searching it or copying it. The grid itself does not change. Different traversals are different orders through the same cells.
Which matrix traversal is the fastest?
For the same number of cells every full traversal takes the same number of steps, rows × columns. In practice row-major is usually fastest on real hardware because most languages store a 2D array row by row, so it reads memory in a straight line. Boundary traversal is faster only because it visits fewer cells.
Do all traversals visit every cell?
All of them here except boundary traversal, which walks only the outer ring and skips the inside. Its visit count is 2 × rows + 2 × columns − 4 on any grid with at least two rows and two columns.
Can I traverse my own grid?
Yes. Paste or build a grid in the 2D Array Visualizer, then use the Traverse this grid links under it. Every traversal page also has row and column sliders, and the Copy link button gives you a URL that reopens the same grid at the same step.