Ordered Matrix
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.Explanation
For example, if the given M is 3 and N is 3, read the inputs in the next three lines if the numbers given in the next three lines are the following.
1 20 3
30 10 2
5 11 15
By ordering all the elements of the matrix in increasing order, the ordered matrix should be
1 2 3
5 10 11
15 20 30
Sample Input 1
3 3
1 20 3
30 10 2
5 11 15
Sample Output 1
1 2 3
5 10 11
15 20 30
Sample Input 2
2 5
-50 20 3 25 -20
88 17 38 72 -10
Sample Output 2
-50 -20 -10 3 17
20 25 38 72 88
SIZE = 10
def sortingMatrix(matrix, x) :
tempData = [0] * (x * x)
count = 0
for i in range(0, x) :
for j in range(0, x) :
tempData[count] = matrix[i][j]
count += 1
tempData.sort()
count = 0
for i in range(0, x) :
for j in range(0, x) :
matrix[i][j] = tempData[count]
count += 1
def printingMatrix(matrix, x) :
for row in range(0, x) :
for column in range( 0, x ) :
print(matrix[row][column] , end = " ")
print()
matrix = [ [ 1, 20, 3 ],
[ 30, 10, 2 ],
[ 5, 11, 15 ] ]
x = 3
print( "Sample input:")
printingMatrix(matrix, x)
sortingMatrix(matrix, x)
print("\nSample Output:")
printingMatrix(matrix, x)
Comments
Leave a comment