Given a M x N matrix, write a program to print the matrix after ordering all the elements of the matrix in increasing order.
M=int(input(""))
N=int(input(""))
numbers= []
#Get numbers from the user
for i in range(0,M):
valuesStr=input("")
values=valuesStr.split()
for v in values:
numbers.append(int(v))
#sort
numbers.sort()
numbers2D=[]
numbers2D = [numbers[r*N:(r+1)*N] for r in range(0,M)]
print()
#Display result
for row in numbers2D:
for elem in row:
print(elem, end=' ')
print()
Comments
Please explain this code
Leave a comment