Python Program to Print Matrix after ordering all elements in increasing order

Ordered Matrix in Increasing order

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 21 4
40 20 3
6 11 15

Sample Output 1
1 3 4
6 11 15
20 21 40

Code:

m,n=input().split()
m=int(m)
n=int(n)
num_list=[]
for i in range(m):
    lines=input().split()
    for j in lines:
        j=int(j)
        num_list+=[j]
num_list.sort()
a=0
b=n
for rows in range(m):
    result=(num_list[a:b])
    for i in result:
        print(i,end=" ")
    print()
    a+=n
    b+=n
        
        
    

Input
3 4
1 21 4 9
40 20 3 12
6 11 15 18

Output
1 3 4 6
9 11 12 15
18 20 21 40

Related Posts

Python Program to Convert Integer into a Single Numeral

Single Digit Number Ram is given a positive integer N .He wishes to convert this integer into a single numeral. He does so by repeatedly adding the…

Python Program to Print Armstrong numbers between two intervals

Write a program to print all the Armstrong numbers in the given range A to B (including A and B). An N – digit number is an…

Python Program to Check if Given Input is a Letter, Digit or Special Character

Letter, Digit or Special Character You are given a character as input. Check if the given input is a Lowercase Letter or Uppercase Letter or Digit or…

Interleave Strings | Python Program to merge two strings by adding characters in alternating order

Interleave Strings Given two strings, write a program to merge the given two strings by adding characters in alternating order, starting with the first string. If a…

Python Program to Check if given colors are present in the color palate

Shekhar is an artist. He likes to draw and paint. He is now drawing a portrait of a city road which is in the black color and…

Python Program to Check the eligibility criteria for donor  

Python Comparison Operators An XYZ hospital has organised a blood donation camp. The eligibility criteria for donor was as follows  1) Age>=18  2) Weight >=50 kg  3)…

Leave a Reply

Your email address will not be published.