Walk a matrix in spiral order by shrinking four bounds, and turn a square matrix a quarter turn in place, ring by ring or by transposing and reversing rows. Against a grid of seen marks and a copy: the same O(n²) time, without the extra n² cells.
This interactive visualization needs JavaScript.