Matrix Column-wise Sort
Matrix Column-wise Sort
Problem:
Given a matrix M containing the elements in R rows and C columns. Sort the matrix elements in ascending order along each column. Print the column-wise sorted matrix in the matrix format.
Input Format:
The first line contains the values of R and C separated by a space. The next R lines (denote the values of matrix M) each containing C values separated by a space.
Output Format:
R lines contain the column-wise sorted matrix each containing C values separated by a space.
Boundary Conditions: 1 < R, C < 50
Example Input/Output 1:
Input:
2 2
1 0
0 1
Output:
0 0
1 1
Example Input/Output 2:
Input:
3 5
4 8 9
4 1 2
6 1 9
4 0 5
4 2 6
Output:
0 5 1
2 1 2
6 4 4
4 4 8
9 9 6
Program:
r,c = map(int,input().split())
l = []
for i in range(r):
l.append(list(map(int,input().split())))
transpose = []
for i in range(c):
k = []
for j in range(r):
k.append(l[j][i])
transpose.append(sorted(k))
for i in range(r):
for j in range(c):
print(transpose[j][i],end=" ")
print()
Comments
Post a Comment