In computing, row-major order and column-major order are methods for storing multidimensional arrays in linear storage such as random access memory.

The two mentioned ways differ from each other with respect to the order in which elements are stored contiguously in the memory. The elements in row-major order are arranged consecutively along the row and that in the column-major order are arranged consecutively along the column. While the terms allude to the rows and columns of a two-dimensional array, i.e. a matrix, the orders can be generalized to arrays of any dimension by noting that the terms row-major and column-major are equivalent to lexicographic and lexicographic orders, respectively.

Below program illustrates that row major order storing of arrays in C is more efficient than column-major order(though Pascal and Fortran follows column major order):

`#include <stdio.h> ` `#include <time.h> ` `int` `m[9999][999]; `
` ` `void` `main() `
` ` `{ ` ` ` `int` `i, j; `
` ` `clock_t` `start, stop; `
` ` `double` `d = 0.0; `
` ` ` ` `start = ` `clock` `(); `
` ` `for` `(i = 0; i < 9999; i++) `
` ` `for` `(j = 0; j < 999; j++) `
` ` `m[i][j] = m[i][j] + (m[i][j] * m[i][j]); `
` ` ` ` `stop = ` `clock` `(); `
` ` `d = (` `double` `)(stop - start) / CLOCKS_PER_SEC; `
` ` `printf` `(` `"The run-time of row major order is %lf\n"` `, d); `
` ` ` ` `start = ` `clock` `(); `
` ` `for` `(j = 0; j < 999; j++) `
` ` `for` `(i = 0; i < 9999; i++) `
` ` `m[i][j] = m[i][j] + (m[i][j] * m[i][j]); `
` ` ` ` `stop = ` `clock` `(); `
` ` `d = (` `double` `)(stop - start) / CLOCKS_PER_SEC; `
` ` `printf` `(` `"The run-time of column major order is %lf"` `, d); `
`} ` |

*chevron_right*

*filter_none*

**Output:**

The run-time of row major order is 0.067300 The run-time of column major order is 0.136622

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Number of positions with Same address in row major and column major order
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Search in a sorted 2D matrix (Stored in row major order)
- Check if row-major order path of Matrix is palindrome or not
- Print all elements in sorted order from row and column wise sorted matrix
- heapq in Python to print all elements in sorted order from row and column wise sorted matrix
- Maximum path sum that starting with any cell of 0-th row and ending with any cell of (N-1)-th row
- Count number of islands where every island is row-wise and column-wise separated
- Find sum of all elements in a matrix except the elements in row and/or column of given cell?
- Given a Boolean Matrix, find k such that all elements in k'th row are 0 and k'th column are 1.
- Count Negative Numbers in a Column-Wise and Row-Wise Sorted Matrix
- Count zeros in a row wise and column wise sorted matrix
- Sum of matrix in which each element is absolute difference of its row and column numbers
- Sum of matrix element where each elements is integer division of row and column
- Check if a grid can become row-wise and column-wise sorted after adjacent swaps
- Check if sums of i-th row and i-th column are same in matrix
- Minimum operations required to make each row and column of matrix equals
- Sort the matrix row-wise and column-wise
- Sum of middle row and column in Matrix
- Program to find the Sum of each Row and each Column of a Matrix