# Difference between Row Major Order and Column Major order

Let take a multi-dimensional array: $a[2][3][4]$

Here $a$ is a three-dimensional array, which means that it is a collection of two-dimensional array's.

Therefore sizeof(a) = No.of 2D arrays * Size of each 2D array = $2*(3*4) = 24$ elements.

Now my observations are:

1. If Row Major Order is taken, then we have 2 arrays of a[3][4] so each array has $12$ elements.

2. If we take Column Major Order, then we have 4 arrays of a[2][3]; that is we have only $6$ elements per array.

Is this approach of calculation right?

• What do you mean by sizeof? When you write sizeof(), are you asking about what sizeof() returns in a C program? The answer might depend on what programming language you are using, so are you asking about a particular language? I'm not sure that's on-topic here; coding questions are off-topic here. What's RMO? What's CMO? Please define or spell out all acronyms.
– D.W.
Aug 19, 2018 at 16:36
• @D.W. definitions of CMO/RMO are in the question header Aug 20, 2018 at 8:57
• sir my doubt is, if it is column major order.... did we have 4 [2][3] arrays ? or 2 [3][4] arrays? Aug 20, 2018 at 17:19

The only difference between Row Major Order and Column Major Order in the case of ANSI C is that the memory access patterns are different.

Suppose we have an array $a[i][j]$ which is stored in Row Major Order and we decide to iterate by columns, then the CPU would take more time to process this set of instruction as compared to the case where we decide to iterate by rows. Using the correct access pattern helps the Operating System use caches more effectively.

However this has no bearing on the running time complexity of the code and for all arrays which occupy space below a certain threshold, the difference would be hardly noticeable.

One final note:

1. If Row Major Order is taken, then we have $2$ arrays of $a[3][4]$ so each array has $12$ elements. Total no.of elements $= 2*12 = 24$ elements.
2. If Column Major Order is taken, then we have $4$ arrays of $a[2][3]$ so each array has $6$ elements. Total no.of elements $= 4*6 = 24$ elements.
• sir my doubt is, if it is column major order.... did we have 4 [2][3] arrays ? or 2 [3][4] arrays? Aug 20, 2018 at 17:16

Consider this :

int[][] data = {1,2,3}, {4,5,6}, {7,8,9};
for(int i = 0; i < data.length; i++){ //Rows
for(int j = 0; j < data[i].length; j++){ //Columns
int elements = data[i][j];
System.out.print(elements + " ");
}
System.out.println();
}


In a Row-Major traversal of an array, the elements are stored in the following order in the memory: 1,2,3,4,5,6,7,8,9. So, the first element will be data[0][0], second element will be data[0][1], third element will be data[0][2], … and the last element being data[2][2].

On the other hand, in a Column-Major traversal,

int[][] data = {1,2,3}, {4,5,6}, {7,8,9};
for(int j = 0; j < data[0].length; j++){ //Columns
for(int i = 0; i < data.length; i++){ //Rows
int elements = data[i][j];
System.out.print(elements + " ");
}
System.out.println();
}


the elements are stored in the memory as follows: 1,4,7,2,5,8,3,6,9. So, the first element will be: data[0][0], second element will be data[1][0], third will be data[2][0], … and the last being data[2][2].

• pseudocode is the preferred procedural notation here. Mar 21, 2023 at 21:26

Speaking of $$2$$ arrays $$[3][4]$$ vs. $$4$$ arrays $$[2][3]$$ is misleading, as it might be understood with the same storage order for all arrays.

The truth is closer to $$2$$ arrays of $$3$$ arrays of $$4$$ elements vs. $$4$$ arrays of $$3$$ arrays of $$2$$ elements, if that is understood as the storage order.

Then the commutativity/associativity rule $$2\cdot(3\cdot4)=4\cdot(3\cdot2)$$ obviously holds.

There are six possible ways to permute three indexes, but only two are considered: direct and reverse.

In 3D, the terminology row/column is not so helpful because there is no universal orientation convention and no unique word for the third dimension (can be layer).