# Row-wise vs column-wise traversal of matrix

Two common ways of traversing a matrix are row-major-order and column-major-order.

**Row Major Order : **When matrix is accessed row by row.

**Column Major Order : **When matrix is accessed column by column.

Examples:

Input : mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}} Output : Row-wise: 1 2 3 4 5 6 7 8 9 Col-wise : 1 4 7 2 5 8 3 6 9

**Difference:** If we see according to time complexity, both lead to **O(n ^{2})**, but when it comes to cache level one of the orders access will be faster as compare to other one. It depends on the language we are using. Like in

**C**, store matrix in row major form so while accessing the i+1

^{th}element after i

^{th}, most probably it will lead to a hit, which will further reduce the time of program.

Following is C code showing the time difference in row major and column major access.

`// C program showing time difference ` `// in row major and column major access ` `#include <stdio.h> ` `#include <time.h> ` ` ` `// taking MAX 10000 so that time difference ` `// can be shown ` `#define MAX 10000 ` ` ` `int` `arr[MAX][MAX] = {0}; ` ` ` `void` `rowMajor() { ` ` ` ` ` `int` `i, j; ` ` ` ` ` `// accessing element row wise ` ` ` `for` `(i = 0; i < MAX; i++) { ` ` ` `for` `(j = 0; j < MAX; j++) { ` ` ` `arr[i][j]++; ` ` ` `} ` ` ` `} ` `} ` ` ` `void` `colMajor() { ` ` ` ` ` `int` `i, j; ` ` ` ` ` `// accessing element column wise ` ` ` `for` `(i = 0; i < MAX; i++) { ` ` ` `for` `(j = 0; j < MAX; j++) { ` ` ` `arr[j][i]++; ` ` ` `} ` ` ` `} ` `} ` ` ` `// driver code ` `int` `main() { ` ` ` `int` `i, j; ` ` ` ` ` `// Time taken by row major order ` ` ` `clock_t` `t = ` `clock` `(); ` ` ` `rowMajor(); ` ` ` `t = ` `clock` `() - t; ` ` ` `printf` `(` `"Row major access time :%f s\n"` `, ` ` ` `t / (` `float` `)CLOCKS_PER_SEC); ` ` ` ` ` `// Time taken by column major order ` ` ` `t = ` `clock` `(); ` ` ` `colMajor(); ` ` ` `t = ` `clock` `() - t; ` ` ` `printf` `(` `"Column major access time :%f s\n"` `, ` ` ` `t / (` `float` `)CLOCKS_PER_SEC); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

Row major access time :0.492000 s Column major access time :1.621000 s

## Recommended Posts:

- Zigzag (or diagonal) traversal of Matrix
- Element in a matrix starting from which anti-clockwise traversal ends at the last element
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Program to check diagonal matrix and scalar matrix
- Circular Matrix (Construct a matrix with numbers 1 to m*n in spiral way)
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N matrix
- Count frequency of k in a matrix of size n where matrix(i, j) = i+j
- Program to convert given Matrix to a Diagonal Matrix
- Check if it is possible to make the given matrix increasing matrix or not
- Program to check if a matrix is Binary matrix or not
- Maximum trace possible for any sub-matrix of the given matrix
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Find sub-matrix with the given sum
- Sort the given matrix
- Hilbert Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.