Given a M x N matrix, write a program to print the matrix after ordering all the elements of the matrix in increasing order.Input
The first line of input will contain two space-separated integers, denoting the M and N.
The next M following lines will contain N space-separated integers, denoting the elements of each list.Output
The output should be M lines containing the ordered matrix.
Note: There is a space at the end of each line.
M, N = input().split(' ')
M, N = int(M), int(N)
matrix = []
for i in range(M):
row = [int(j) for j in input().split(' ')]
matrix.append(row)
values = [j for row in matrix for j in row]
values.sort()
output = matrix
for i in range(M):
for j in range(N):
output[i][j] = values[i * M + j]
print(output)
Comments
Leave a comment