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]]
Output : [[10, 18, 3], [9, 10, 3], [0, 3, 5]]
Explanation : 18, 10, and 5 are maximum elements in rows, hence sorted.
Method #1 : Using sort() + max()
In this, we perform task of sorting using sort() with key being maximum element from each row. The reverse keyword is used to sort by keeping maximum element rows at start and decreasing from there.
Python3
def max_sort(row):
return max (row)
test_list = [[ 5 , 7 , 8 ], [ 9 , 10 , 3 ],
[ 10 , 18 , 3 ], [ 0 , 3 , 5 ]]
print ( "The original list is : " + str (test_list))
test_list.sort(key = max_sort, reverse = True )
print ( "The maximum sorted Matrix : " + str (test_list))
|
Output:
The original list is : [[5, 7, 8], [9, 10, 3], [10, 18, 3], [0, 3, 5]] The maximum sorted Matrix : [[10, 18, 3], [9, 10, 3], [5, 7, 8], [0, 3, 5]]
Time Complexity: O(nlogn) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list “test_list”.
Method #2 : Using sorted() + lambda + max()
In this, we perform task of sorting using sorted() for non-inplace sort, and lambda function is used instead of external function to include maximum element from row logic.
Python3
test_list = [[ 5 , 7 , 8 ], [ 9 , 10 , 3 ],
[ 10 , 18 , 3 ], [ 0 , 3 , 5 ]]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda row : max (row), reverse = True )
print ( "The maximum sorted Matrix : " + str (res))
|
Output:
The original list is : [[5, 7, 8], [9, 10, 3], [10, 18, 3], [0, 3, 5]] The maximum sorted Matrix : [[10, 18, 3], [9, 10, 3], [5, 7, 8], [0, 3, 5]]
Method 3: Using a for loop
Step-by-step approach:
- Initialize the given matrix test_list.
- Iterate through each row of the matrix using a for loop.
- For each row, find the maximum element using the max() function and store it in a list max_list.
- Use the zip() function to pair the elements of max_list and test_list row-wise.
- Sort the paired elements based on the values of max_list in descending order using the sorted() function and list comprehension.
- Unzip the sorted pairs using the zip() function and store only the second element of each pair (i.e. the sorted rows of the matrix) in a list res.
- Print the sorted matrix res.
Python3
test_list = [[ 5 , 7 , 8 ], [ 9 , 10 , 3 ],
[ 10 , 18 , 3 ], [ 0 , 3 , 5 ]]
print ( "The original list is : " + str (test_list))
max_list = []
for row in test_list:
max_list.append( max (row))
res = [x for _, x in sorted ( zip (max_list, test_list), reverse = True )]
print ( "The maximum sorted Matrix : " + str (res))
|
Output
The original list is : [[5, 7, 8], [9, 10, 3], [10, 18, 3], [0, 3, 5]]
The maximum sorted Matrix : [[10, 18, 3], [9, 10, 3], [5, 7, 8], [0, 3, 5]]
Time complexity: O(n^2) as we need to iterate through each element of the matrix and find the maximum element of each row.
Auxiliary space: O(n) to store the maximum values of each row in a list.
Last Updated :
01 May, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...