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]
for row in output:
print(*row)
input:
2 5
-50 20 3 25 -20
88 17 38 72 -10
output:-
50 -20 -10 3 17
-10 3 17 20 25
the output should be like this:-
50 -20 -10 3 17
20 25 38 72 88
so can anyone please give correct code?
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)
output=[]
for i in range(M):
for j in range(N):
output.append(matrix[i][j])
print()
output.sort()
n=0
for i in range(M):
for j in range(N):
print(output[n],end=" ")
n=n+1
print()
Comments
Leave a comment