Test Case
Below is the test case.
Input
{
{1,2,3},
{4,5,6},
{7,8,9}
}
Output
Rowwise traversal: {1,2,3,4,5,6,7,8,9}
Columnwise traversal: {1,4,7,2,5,8,3,6,9}
Explanation
Given Matrix is,
For the Rowwise traversal, you need to print row 1 first, row 2, and row 3, as shown below.
Hence, the output is {1,2,3,4,5,6,7,8,9}.
For the Columnwise traversal, you need to print col1 first, then col 2, and then col 3, as shown below.
Hence the output is {1,4,7,2,5,8,3,6,9}.
We hope that the question is clear to you. Let’s now move to the solution part.
Solution
This problem has a simple solution. You just need to traverse through the whole matrix.
For rowwise traversal, first, you need to visit each row one by one, starting from the first to the last row, then print values in each row.
For columnwise traversal, you need to visit each column, starting from the first to the last, and then print each column's elements.
Below is the algorithm.
Algorithm

Create an 2D array(arr[][]) of size MxN, for representing the 2D Matrix.

For rowwise traversal, start a loop from i = 0 to M (traversing row by row) and for each iteration of the loop(for each row), start a nested loop from j = 0 to N(traverse each elements of the row) and print the element at arr[i][j].
 For columnwise traversal, start a loop from i=0 to N(traversing column by column) and fro each iteration of the loop(for each column), start a nested loop from j = 0 to M(traverse each elements of the column) and orint element ar arr[j][i].
Implementation
Now, we will implement the above algorithm in different languages.
C++ Code
#include<bits/stdc++.h>
using namespace std;
int main() {
int m = 3;
int n = 3;
int arr[m][n] = {{1 , 2 , 3},
{4 , 5 , 6},
{7 , 8 , 9}};
// Rowwise traversal
cout << "Rowwise traversal: ";
for(int i = 0 ; i < m ; i++) {
// for each rows(i = 0 to m) we are printing the values
for(int j = 0; j < n ; j++) {
cout << arr[i][j] <<" ";
}
}
//Columnwise traversal
cout << "\nColumnwise traversal: ";
for(int i = 0; i < n ; i++) {
// for each columns(i = 0 to n) we are printing values
for(int j = 0 ;j < m ; j++) {
cout << arr[j][i] <<" ";
}
}
return 0;
}
You can also try this code with Online C++ Compiler
Run Code
Java Code
class Main{
public static void main(String[] args) {
// given matrix
int m = 3;
int n = 3;
int[][] arr = {{1, 2, 3} , {4, 5, 6} , {7, 8, 9}};
// Rowwise traversal
System.out.print("Rowwise traversal: ");
for(int i = 0; i < m ; i++) {
for(int j = 0; j < n ; j++) {
System.out.print(arr[i][j] + " ");
}
}
// Columnwise traversal
System.out.print("\nColumnwise traversal: ");
for(int i = 0; i < m ; i++) {
for(int j = 0; j < n ; j++) {
System.out.print(arr[j][i] + " ");
}
}
}
}
You can also try this code with Online Java Compiler
Run Code
Python Code
m = 3
n = 3
arr = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
# Rowwise traversal
print("Rowwise traversal:", end = " ")
for i in range(0 , m):
for j in range(0 , n):
print(arr[i][j],end=" ")
# Columnwise traversal
print("\nColumnwise traversal:", end=" ")
for i in range(0 , m):
for j in range(0 , n):
print(arr[j][i], end=" ")
You can also try this code with Online Python Compiler
Run Code
Output
Rowwise traversal: 1 2 3 4 5 6 7 8 9
Columnwise traversal: 1 4 7 2 5 8 3 6 9
Complexity Analysis
Time complexity: O(MN)
Space complexity: O(1)
Rowwise vs Columnwise Traversal
As we saw earlier, both the rowwise and columnwise traversal has a time complexity O(MN). But, if we want to compare the performance between these traversals, you will find that one of the traversals becomes slower than the other.
An algorithm's performance also depends on how the elements are accessed. In this case, it depends on how the matrix is stored in the memory.

If the matrix is stored in the memory in rowmajor order(often), consecutive elements of the rows are contiguous in memory. Reading memory in contiguous memory locations is faster than jumping to other locations. So, here rowmajor traversal will give better performance than the columnmajor traversal.
 Similarly, suppose the matrix is stored in the memory in columnmajor order. In that case, the columnmajor traversal will perform better than the rowmajor traversal for the same reason mentioned above.
Also, see Array in Java
Frequently Asked Questions
What is rowmajor traversal?
While traversing a 2D matrix, if we traverse the matrix row by row, it is called rowmajor traversal.
What is columnmajor traversal?
While traversing a 2D matrix, if we traverse the matrix column by column, it is called columnmajor traversal.
Which one of the traversal among rowmajor and columnmajor gives better performance for traversing a matrix?
It depends on how the matrix is stored in the memory. If the matrix is stored in the memory in rowmajor order, then the rowmajor traversal will perform better than the columnmajor traversal and vice versa.
What is the time and space complexity of traversing a matrix?
Time complexity: O(MN)
Space complexity: O(1)
Conclusion
In this article, we have extensively discussed Rowwise vs Columnwise Traversal of a Matrix.
We started with the basic introduction, then we discussed,
 The Problem Statement
 One Test Case
 Solution

Finally, we discussed the difference between these two traversals.
We hope this blog has helped you enhance your knowledge regarding Rowwise vs Columnwise Traversal of a Matrix.
Recommended problems 
If you want to learn more, follow our articles on Operation On Matrices, How to Multiply Two Matrices, and Minimum Sum Path in a Matrix.
Explore our practice platform Coding Ninjas Studio to practice top problems, attempt mock tests, read interview experiences, interview bundle, follow guided paths for placement preparations, and much more.!
Please upvote this blog to help other ninjas grow.
Happy Reading!