VOOZH about

URL: https://www.geeksforgeeks.org/dsa/sort-the-matrix-column-wise/

⇱ Sort the matrix column-wise - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Sort the matrix column-wise

Last Updated : 3 Oct, 2025

Given a matrix mat[][] of dimensions N * M, the task is to sort each column of a matrix in ascending order and print the updated matrix.

Examples:

Input: mat[][] = { {1, 6, 10}, {8, 5, 9}, {9, 4, 15}, {7, 3, 60} }
Output:
1 3 9
7 4 10
8 5 15
9 6 60
Explanation:
The input matrix is sorted in a column wise manner,  
1 < 7 < 8 < 9
3 < 4 < 5 < 6
9 < 10 < 15 < 60

Input: arr[][] = { {1, 6}, {8, 4}, {9, 7} }
Output:
1 4
8 6
9 7

Approach: Follow the steps below to solve the problem:

Below is the implementation of the above approach:


Output: 
1 3 9 
7 4 10 
8 5 15 
9 6 60

 

Time Complexity: O(N * M)
Auxiliary Space: O(N * M)


 

Comment