I think my algorithms are simpler and easier to understand than those on the web. As for the complexity of time, there is no comparison.
Algorithm idea: Traversing the matrix from the outermost to the inner layer
# My algorithm
Import Mathdef Print_matrix (matrix): rec = [] # Store traversal element rows = Len (Matrix) cols = Len (matrix[0]) if rows &L T cols: k = Math.ceil (ROWS/2) else: k = Math.ceil (COLS/2) for N in range (k): # Total number of loops # the top row loop C10/>i = j = N while J < cols: rec.append (Matrix[i][j]) # Print (Matrix[i][j]) J + = 1 # Right-most column follow Ring J-= 1 i + = 1 while i < rows: rec.append (Matrix[i][j]) # Print (matrix[i][j]) i + = 1< c22/># the bottom line loop I-= 1 while j > N: J-= 1 rec.append (Matrix[i][j]) # Print (Matrix[i][j]) c28/># leftmost column loop while i > n+1: i-= 1 rec.append (Matrix[i][j]) # Print (matrix[i][j]) rows- = 1 cols-= 1 return rec
Print the matrix element clockwise (Python implementation)