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
Sample input1
3 3
1 20 3
30 10 2
5 10 15
Sample output
1 2 3
5 10 11
15 20 30
M,N = input().split()
M = int(M)
N = int(N)
print()
matrix = []
for i in range(M):
row = input().split()
numbers = [int(number) for number in row]
matrix.append(numbers)
matrix_to_list = []
for numbers in matrix:
for number in numbers:
matrix_to_list.append(number)
matrix_to_list.sort()
index_of_number = 0
for i in range(M):
for j in range(N):
matrix[i][j] = matrix_to_list[index_of_number]
index_of_number +=1
print()
print("Output")
for numbers in matrix:
for number in numbers:
print(number, end=' ')
prin
Comments
Leave a comment