Python program to Sort Columns of Element by Sorting the First Row

from random import randint
col = 6
row = 3
matrix = []
for i in range(row):
 myrow = []
 for j in range(col):
    myrow.append(randint(10,100))
 matrix.append(myrow)
for i in matrix:
 print(i)
print()
print('Result of sorted column of elements below')
k = col-1
while k != 0:
 z = 0
 for j in range(1, k+1):
     if matrix[0][j] > matrix[0][z]:
      z = j
 for i in range(row):
     y = matrix[i][z]
     matrix[i][z] = matrix[i][k]
     matrix[i][k] = y
 k -= 1
for i in matrix:
 print(i)

 

 

 

Output:

 

[56, 76, 22, 42, 77, 38]
[92, 93, 98, 11, 15, 78]
[14, 86, 76, 17, 43, 21]

Result of sorted column of elements below
[22, 38, 42, 56, 76, 77]
[98, 78, 11, 92, 93, 15]
[76, 21, 17, 14, 86, 43]

 

Subscribe For Daily Updates