how to sort the m*n matrix elements in ascending order
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)
Comments
Leave a comment