Answer to Question #224224 in Python for satya

Question #224224

how to sort the m*n matrix elements in ascending order


1
Expert's answer
2021-08-09T16:39:13-0400
SIZE = 10
def sortMatrix(m, n) :
    temp = [0] * (n * n)
    k = 0
 
    for i in range(0, n) :
         
        for j in range(0, n) :
             
            temp[k] = m[i][j]
            k += 1
 
    temp.sort()
    k = 0
     
    for i in range(0, n) :
         
        for j in range(0, n) :
            m[i][j] = temp[k]
            k += 1


def printMatrix(m, n) :
     
    for i in range(0, n) :
         
        for j in range( 0, n ) :
             
            print(m[i][j] , end = " ")
             
        print()
     
     
matrix = [ [ 15, 14, 17 ],
        [ 11, 13, 18 ],
        [ 12, 19, 16 ]
        ]
n = 3
 
print( "Original:")
printMatrix(matrix, n)
 
sortMatrix(matrix, n)
 
print("\nAfter Sorting:")
printMatrix(matrix, n)
 

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS