Column Major Order in Data Structure with Example

Column Major Order is a way to represent the multidimensional array in sequential memory. It has similar functionality as row-major order, but the way of process is different.

In Column Major Order, elements of a multidimensional array are arranged sequentially column-wise which means filling all the index of the first column and then move to the next column.

Let’s see an example

Suppose we have some elements {1,2,3,4,5,6,7,8} which we want to insert in an array by following column-major order.

So If we insert these elements in column-major order, our 2-D array will look like

Here first fill index[0][0] and then index[1][0] which is just opposite to the row-major where we first fill all row nodes then move to the next row.
And then for second-column index[0][1] and then index[1][1], index[0][2], index[1][2] and so on.

Column Major Order Formula

The Location of element A[i, j] can be obtained by evaluating expression:

LOC (A [i, j]) = base_address + W * [M * j + i]


base_address = address of the first element in the array.

W = Word size means a number of bytes occupied by each element of an Array.

M = Number of rows in the array.

N = Number of columns in the array.

Note: Array Index starts from 0.

Here we have taken a 2-D array A [2, 4] which has 2 rows and 4 columns.

Suppose we want to calculate the address of element A [1, 2] in column-major order and the matrix is 2*4. It can be calculated as follow:

Now to calculate the base address of any index using column-major order we can use the process given below.

It can be calculated as follow:
base_address = 2000, W= 2, M=2, N=4, i=1, j=2
LOC (A [i, j]) = base_address + W [M * j + i ]
LOC (A[1, 2]) = 2000 + 2 [2*2 + 1]
= 2000 + 2 * [4 + 1]
= 2000 + 2 * 5
= 2000 + 10
= 2010