Python - Sort Matrix by Row Median
Last Updated :
08 Mar, 2023
Given a Matrix, sort by median of each row.
Input : test_list = [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
Output : [[1, 7, 2], [3, 4, 7], [10, 2, 4], [8, 6, 5]]
Explanation : 2 < 3 < 4 < 6, sorted increasingly by median element.
Input : test_list = [[3, 4, 7], [1, 7, 2], [8, 6, 5]]
Output : [[1, 7, 2], [3, 4, 7], [8, 6, 5]]
Explanation : 2 < 3 < 6, sorted increasingly by median element.
Method #1 : Using sort() + median()
In this, we perform sort using sort() and median is computed using statistics function of computing median, median().
Python3
# Python3 code to demonstrate working of
# Sort Matrix by Row Median
# Using sort() + median()
from statistics import median
def med_comp(row):
# computing median
return median(row)
# initializing list
test_list = [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
# printing original list
print("The original list is : " + str(test_list))
# inplace sorting using sort()
test_list.sort(key = med_comp)
# printing result
print("Sorted Matrix : " + str(test_list))
OutputThe original list is : [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
Sorted Matrix : [[1, 7, 2], [3, 4, 7], [10, 2, 4], [8, 6, 5]]
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Method #2 : Using sorted() + lambda + median()
In this, we perform task of perform sort using sorted(), and lambda function is used as key function rather than external function.
Python3
# Python3 code to demonstrate working of
# Sort Matrix by Row Median
# Using sorted() + lambda + median()
from statistics import median
# initializing list
test_list = [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
# printing original list
print("The original list is : " + str(test_list))
# inplace sorting using sort()
res = sorted(test_list, key = lambda row : median(row))
# printing result
print("Sorted Matrix : " + str(res))
OutputThe original list is : [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
Sorted Matrix : [[1, 7, 2], [3, 4, 7], [10, 2, 4], [8, 6, 5]]
Time Complexity: O(n*nlogn) where n is the number of elements in the list “test_list”. sorted() + lambda + median() performs n*nlogn number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #3 : Using heapq.nsmallest() and statistics.median() function:
Algorithm:
1.Import the necessary modules heapq and statistics.
2.Initialize the input list.
3.For each row in the input list, calculate the median of the row using the median() function from the statistics module.
4.Sort the input list using heapq.nsmallest() function and pass the length of the input list, the input list and a lambda function that returns the 5.median of each row as the key to sort.
6.The sorted matrix is returned as output.
Python3
import heapq
from statistics import median
# initializing list
test_list = [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
# printing original list
print("The original list is : " + str(test_list))
# get the row median and use heapq.nsmallest() to sort
res = heapq.nsmallest(len(test_list), test_list, key=lambda row: median(row))
# printing result
print("Sorted Matrix : " + str(res))
#This code is contributed by Jyothi pinjala.
OutputThe original list is : [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]]
Sorted Matrix : [[1, 7, 2], [3, 4, 7], [10, 2, 4], [8, 6, 5]]
Time Complexity:
The time complexity of the program is O(n * m log m) where n is the number of rows and m is the number of columns in the matrix. The median() function takes O(m log m) time to sort each row, and the nsmallest() function takes O(n * m log m) time to sort the entire matrix.
Auxiliary Space:
The space complexity of the program is O(n * m) as we are creating a new sorted matrix.
Similar Reads
Python - Sort row by K multiples Given a Matrix, perform row sorting by number of multiple of K present in row. Input : test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]], K = 4 Output : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]] Explanation : 0 < 1 < 2 < 4, multiple of 4 occurrence order. I
3 min read
Python Program to Find median in row wise sorted matrix We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples: Input : 1 3 5 2 6 9 3 6 9Output : Median is 5If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9)Input: 1 3 4 2 5 6 7 8 9Output: Median is
5 min read
Python program to Sort Matrix by Maximum Row element Given a Matrix, sort rows by maximum element. Input : test_list = [[5, 7, 8], [9, 10, 3], [10, 18, 3], [0, 3, 5]] Output : [[10, 18, 3], [9, 10, 3], [5, 7, 8], [0, 3, 5]] Explanation : 18, 10, 8 and 5 are maximum elements in rows, hence sorted. Input : test_list = [[9, 10, 3], [10, 18, 3], [0, 3, 5]
4 min read
Python - Filter Sorted Rows Given a Matrix, extract rows that are sorted, either by ascending or descending. Input : test_list = [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]] Output : [[3, 6, 8, 10], [8, 5, 3, 2]] Explanation : Both lists are ordered, 1st ascending, second descending. Input : test_list = [[3, 6,
3 min read
Python program to sort matrix based upon sum of rows Given a Matrix, perform sort based upon the sum of rows. Input : test_list = [[4, 5], [2, 5, 7], [2, 1], [4, 6, 1]] Output : [[2, 1], [4, 5], [4, 6, 1], [2, 5, 7]] Explanation : 3 < 9 < 11 < 14. Sorted sum. Input : test_list = [[4, 5], [2, 5, 7], [4, 6, 1]] Output : [[4, 5], [4, 6, 1], [2,
6 min read
Python - Sort Matrix by total characters Given a String Matrix, sort by total data, i.e total characters in each row. Input : test_list = [["Gfg", "is", "Best"], ["Geeksforgeeks", "Best"], ["ILvGFG"]] Output : [['ILvGFG'], ['Gfg', 'is', 'Best'], ['Geeksforgeeks', 'Best']] Explanation : 6 < 11 < 17 total characters respectively after
5 min read