Answer to Question #206343 in Python for arjun

Question #206343

Given a M x N matrix, write a program to print the matrix after ordering all the elements of the matrix in increasing order.

3 3

1 2 3

5 10 11

15 20 30


1
Expert's answer
2021-06-13T00:04:39-0400
def main():
    n, m = map(int, input('n, m : ').split())
    l = list()
    for i in range(n):
        l.append(list(map(int, input().split())))
    a = list()
    for item in l:
        for i in item:
            a.append(i)

    a.sort()

    k = 0
    for i in range(n):
        for j in range(m):
            l[i][j] = a[k]
            k += 1

    for i in l:
        for j in i:
            print(j, end=' ')
        print()


if __name__ == '__main__':
    main()

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS