DSA Visualizer
GRID

Snake (Zigzag) Traversal

Left to right, then right to left, like mowing a lawn. The path turns at the end of each row instead of jumping back.

Left to right, then right to left, like mowing a lawn. No jumping back to the start of a row.

0
1
2
3
0
1
2
3
4
1
5
6
7
8
2
9
10
11
12
Visited
Press play to start
Snake order goes left to right on even rows and right to left on odd rows, so the path never jumps back to the start of a row.
0
VisitedVisited
12
LeftRemaining
12
TotalTotal
1 / 17

Pseudocode

Snake
1for i in 0 .. rows-1:
2 if i is even:
3 for j in 0 .. cols-1: visit(a[i][j])
4 else:
5 for j in cols-1 .. 0: visit(a[i][j])

Legend

Visiting nowNew directionVisited

Grid size

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

Tip: Space play/pause · ← → step

Learn snake traversal

Snake traversal alternates direction on every row, like mowing a lawn or an old printer head: left to right, then right to left. The path never jumps back to the start of a row, which is why it is also called zigzag or boustrophedon order.

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 row-major, column-major, 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 snake traversal works

Mow a lawn. You push the mower to the far end, turn around, and come back along the next strip. Nobody walks back to the start of the lawn to begin every strip from the same side. Snake traversal is that idea on a grid: row 0 goes left to right, row 1 goes right to left, row 2 goes left to right again.

Compared with row-major order the set of cells is the same and the number of steps is the same. What changes is the path: consecutive cells are always neighbours, so there is never a long jump from the end of one row to the start of the next.

In the visualizer the direction shows as a variable, and the first cell of each new row is marked as a turn, the moment the path swings around.

The loop shape

The outer loop is unchanged: i from 0 to rows − 1. The inner loop depends on whether the row is even or odd. Even rows (i % 2 == 0) run j upwards; odd rows run it downwards from cols − 1.

If you dislike two inner loops, count steps instead of columns: let n run from 0 to cols − 1 and set j = n on even rows, j = cols − 1 − n on odd rows. One loop, one formula, same path.

A worked example: a 3 × 4 grid

Number the cells 1 to 12 in reading order. Row 0 reads forwards, row 1 backwards, row 2 forwards:

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

Look at the joins: 4 is next to 8, and 5 is next to 9. Every pair of consecutive visits touches. There are only two turns on three rows, and the Visited counter still ends on 12.

Where you'll meet it

  • Printers, plotters and 3D printers. The head sweeps back and forth; returning to the left edge every line would double the travel.
  • Robot vacuums and lawn mowers. Boustrophedon coverage is the standard way to cover a rectangle.
  • Interview questions. “Print the matrix in snake pattern” and “zigzag order” both mean this.
  • Grid puzzles and games. Snakes and Ladders numbers its board exactly this way.

Mistakes beginners make

  • Reversing the values instead of the loop. You do not need to reverse a row; you need to read it from the other end.
  • Starting the odd rows at cols. The last valid column is cols − 1.
  • Checking the wrong index. The parity test is on the row i, not on j.

Rows and columns are not the only lines through a grid. Next: Diagonal traversal →

Snake traversal questions

What is snake traversal of a matrix?

A traversal that walks the first row left to right, the second row right to left, the third left to right, and so on. The path snakes down the grid without ever jumping back to the start of a row. It is also called zigzag or boustrophedon order.

How do I reverse the loop on odd rows?

Check i % 2. On even rows run j from 0 up to cols − 1; on odd rows run j from cols − 1 down to 0. A neat alternative is a single loop over n from 0 to cols − 1 with j = i % 2 == 0 ? n : cols − 1 − n.

Where is snake order used?

Anywhere a moving head should not waste a trip back to the start: dot-matrix and inkjet printers, plotters, CNC machines and 3D printer infill all sweep back and forth. Ancient Greek inscriptions were sometimes written this way too, which is where the name boustrophedon, ox-turning, comes from.

Does snake traversal visit every cell?

Yes, exactly once, so it takes rows × columns steps like row-major order. The only thing that changes is the direction on odd rows, which is why the visualizer marks the first cell of each new row as a turn.