Given an n x n matrix, where every row and column is sorted in nondecreasing order. Print all elements of matrix in sorted order.
Examples:
Input : mat= [[10, 20, 30, 40], [15, 25, 35, 45], [27, 29, 37, 48], [32, 33, 39, 50]] Output : Elements of matrix in sorted order [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50]
This problem has existing solution please refer link. We will solve this problem in python with the same approach of merging two sorted arrays using heapq module.
# Function to print all elements in sorted order # from row and column wise sorted matrix from heapq import merge
def sortedMatrix(mat):
# initialize result variable with first row of matrix
result = mat[ 0 ]
# now traverse through complete matrix
# after first row and merge each row with
# result one by one
# after last operation result will contain
# list of sorted elements of matrix
for row in mat[ 1 :]:
result = list (merge(result,row))
return result
if __name__ = = "__main__" :
mat = [[ 10 , 20 , 30 , 40 ],[ 15 , 25 , 35 , 45 ], \
[ 27 , 29 , 37 , 48 ],[ 32 , 33 , 39 , 50 ]]
print 'Elements of matrix in sorted order'
print sortedMatrix(mat)

Output:
Elements of matrix in sorted order [10, 15, 20, 25, 27, 29, 30, 32, 33, 35, 37, 39, 40, 45, 48, 50]
This article is contributed by Shashank Mishra (Gullu). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a studentfriendly price and become industry ready.
Recommended Posts:
 Print all elements in sorted order from row and column wise sorted matrix
 Count Negative Numbers in a ColumnWise and RowWise Sorted Matrix
 Count zeros in a row wise and column wise sorted matrix
 Largest rowwise and columnwise sorted submatrix
 Maximum sum of any submatrix of a Matrix which is sorted rowwise and columnwise
 Search in a row wise and column wise sorted matrix
 Check if a grid can become rowwise and columnwise sorted after adjacent swaps
 Kth smallest element in a rowwise and columnwise sorted 2D array  Set 1
 Sort the matrix rowwise and columnwise
 Rowwise vs columnwise traversal of matrix
 Count number of islands where every island is rowwise and columnwise separated
 Leftmost Column with atleast one 1 in a rowwise sorted binary matrix
 Leftmost Column with atleast one 1 in a rowwise sorted binary matrix  Set 2
 Check if the given matrix is increasing row and column wise
 Program to print elements of a Matrix rowwise skipping alternate elements
 Removing row or column wise duplicates from matrix of characters
 Merge two sorted arrays in Python using heapq
 Find a common element in all rows of a given rowwise sorted matrix
 Find trace of matrix formed by adding Rowmajor and Columnmajor order of same matrix
 Find sum of all elements in a matrix except the elements in row and/or column of given cell?