VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-elements-sorted-order-row-column-wise-sorted-matrix/

⇱ Print all elements in sorted order from row and column wise sorted matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print all elements in sorted order from row and column wise sorted matrix

Last Updated : 23 Jul, 2025

Given an n x n matrix, where every row and column is sorted in non-decreasing order. Print all elements of the matrix in sorted order.

Example: 

Input: mat[][] = { {10, 20, 30, 40},
                           {15, 25, 35, 45},
                           {27, 29, 37, 48},
                          {32, 33, 39, 50},
                   };
Output: 10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50

Recommended Practice

We can use Young Tableau to solve the above problem. The idea is to consider the given 2D array as Young Tableau and call extract minimum O(N) 

Algorithm:

  1. Define a constant INF equal to INT_MAX and N equal to the size of the matrix.
  2. Implement a youngify function that takes a 2D matrix and two integer values i and j as input parameters.
  3. In the youngify function, find the values at the down and right sides of mat[i][j].
  4. If mat[i][j] is the down right corner element, then return.
  5. Move the smaller of two values (downVal and rightVal) to mat[i][j] and recursively call youngify for the smaller value.
  6. Implement an extractMin function that takes a 2D matrix as an input parameter.
  7. In the extractMin function, store the value at mat[0][0] in a variable ret and update mat[0][0] with INF.
  8. Recursively call youngify with the starting cell (0, 0).
  9. Return the value stored in ret.
  10. Implement a printSorted function that takes a 2D matrix as an input parameter.
  11. Iterate N*N times and call extractMin each time and print the returned value.
  12. Implement the main function.
  13. Declare a 2D matrix mat of size NxN and initialize it with values.
  14. Call the printSorted function with mat as an input parameter.
  15. Return 0

Below is the implementation of this approach:


Output
10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50 

Time complexity of extract minimum is O(N) and it is called O(N2) times. Therefore the overall time complexity is O(N3).
Auxiliary Space: O(N2)

Another approach: The idea is to keep all elements of the matrix in a one-dimensional array and then sort the array and print all values in it.
Below is the implementation of the above approach:


Output
10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50 

Time Complexity: O(N2log(N2))
Auxiliary Space: O(N2)

A better solution is to use the approach used for merging k sorted arrays. The idea is to use a Min Heap of size N which stores elements of first column. They do extract minimum. In extract minimum, replace the minimum element with the next element of the row from which the element is extracted.


Output
10 15 20 25 27 29 30 32 33 35 37 39 40 45 48 50 

Time complexity: O(N2LogN). 
Auxiliary Space: O(N)

Exercise: 
Above solutions work for a square matrix. Extend the above solutions to work for an M*N rectangular matrix.
 

Comment
Article Tags: