Python program to Sort Matrix by Maximum Row element
Last Updated :
01 May, 2023
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
# Python3 code to demonstrate working of
# Sort Matrix by Maximum Row element
# Using sort() + max()
def max_sort(row):
return max(row)
# initializing list
test_list = [[5, 7, 8], [9, 10, 3],
[10, 18, 3], [0, 3, 5]]
# printing original list
print("The original list is : " + str(test_list))
# sort() for sorting, max to get maximum values
test_list.sort(key = max_sort, reverse = True)
# printing result
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
# Python3 code to demonstrate working of
# Sort Matrix by Maximum Row element
# Using sorted() + lambda + max()
# initializing list
test_list = [[5, 7, 8], [9, 10, 3],
[10, 18, 3], [0, 3, 5]]
# printing original list
print("The original list is : " + str(test_list))
# sorted() for sorting, max to get maximum values
# reverse for reversed order
res = sorted(test_list, key = lambda row : max(row), reverse=True)
# printing result
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
# Python3 code to demonstrate working of
# Sort Matrix by Maximum Row element
# Using for loop + max() + sort()
# initializing list
test_list = [[5, 7, 8], [9, 10, 3],
[10, 18, 3], [0, 3, 5]]
# printing original list
print("The original list is : " + str(test_list))
# for loop to iterate through each row and store maximum element in a list
max_list = []
for row in test_list:
max_list.append(max(row))
# sort matrix based on maximum values using zip()
res = [x for _, x in sorted(zip(max_list, test_list), reverse=True)]
# printing result
print("The maximum sorted Matrix : " + str(res))
OutputThe 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.
Similar Reads
Python Program to Find maximum element of each row in a matrix Given a matrix, the task is to find the maximum element of each row.Examples:Â Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Method 1: The idea is to run the loop for no_of_rows. Check each element inside the row and fi
5 min read
Python Program to sort rows of a matrix by custom element count Given Matrix, the following program shows how to sort rows of a matrix by the count of presence of numbers from a specified list. Input : test_list = [[4, 5, 1, 7], [6, 5], [9, 8, 2], [7, 1]], cus_list = [4, 5, 7] Output : [[9, 8, 2], [6, 5], [7, 1], [4, 5, 1, 7]] Explanation : 0 < 1 = 1 < 3 i
5 min read
Python program to Sort Tuples by their Maximum element Given a Tuple List sort tuples by maximum element in a tuple. Input : test_list = [(4, 5, 5, 7), (1, 3, 7, 4), (19, 4, 5, 3), (1, 2)] Output : [(19, 4, 5, 3), (4, 5, 5, 7), (1, 3, 7, 4), (1, 2)] Explanation : 19 > 7 = 7 > 2, is order, hence reverse sorted by maximum element. Input : test_list
5 min read
Python - Sort Matrix by Maximum String Length Given a matrix, perform row sort basis on the maximum length of the string in it. Input : test_list = [['gfg', 'best'], ['geeksforgeeks'], ['cs', 'rocks'], ['gfg', 'cs']] Output : [['gfg', 'cs'], ['gfg', 'best'], ['cs', 'rocks'], ['geeksforgeeks']] Explanation : 3 < 4 < 5 < 13, maximum leng
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 | Row with Minimum element in Matrix We can have an application for finding the lists with the minimum value and print it. This seems quite an easy task and may also be easy to code, but sometimes we need to print the entire row containing it and having shorthands to perform the same are always helpful as this kind of problem can come
5 min read
Python Program to Sort the given matrix Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row âiâ, where 1 <= i <= n-1, first element of row 'i' is greater than or equal to the la
4 min read
Python - Sort by Maximum digit in Element Given a List of Elements, sort by the maximum digit of the element present in the List. Input : test_list = [234, 92, 8, 721] Output : [234, 721, 8, 92] Explanation : 4 < 7 < 8 < 9, sorted by maximum digits. Input : test_list = [92, 8, 721] Output : [721, 8, 92] Explanation : 7 < 8 <
6 min read
Python Program to Sort the matrix row-wise and column-wise Given a n x n matrix. The problem is to sort the matrix row-wise and column wise.Examples: Input : mat[][] = { {4, 1, 3}, {9, 6, 8}, {5, 2, 7} } Output : 1 3 4 2 5 7 6 8 9 Input : mat[][] = { {12, 7, 1, 8}, {20, 9, 11, 2}, {15, 4, 5, 13}, {3, 18, 10, 6} } Output : 1 5 8 12 2 6 10 15 3 7 11 18 4 9 13
4 min read
Python - Sort Matrix by Row Median 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]] Outp
4 min read