Thursday, January 22, 2026
HomeLanguagesPython – Sort Matrix by Number of elements greater than its previous...

Python – Sort Matrix by Number of elements greater than its previous element

Given a Matrix, sort by occurrences where next element is greater than current. Compute the count of i < i + 1 in each list, sort each row by count of each of this condition in each row.

Input : test_list = [[4, 6, 2, 9, 10], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]] 
Output : [[6, 3, 2], [5, 3, 2, 5], [4, 6, 2, 9, 10], [2, 4, 5, 6, 7, 7]] 
Explanation : for [4, 6, 2, 9, 10], the count is 3 as 6>=4, 9>=2 and 10>=9, similarly for [5, 3, 2, 5], [2, 4, 5, 6,  7, 7], [6, 3, 2] counts are 1,4 and 0 respectively. As, 0<1<3<4 so the order of rows is [6, 3, 2], [5, 3, 2, 5], [4, 6, 2, 9, 10], [2, 4, 5, 6, 7, 7]
 

Input : test_list = [[5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]] 
Output : [[6, 3, 2], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7]] 
Explanation : 0 < 1 < 4, is the greater next greater elements count. No next element is greater in 1st list. 
 

Method #1 : Using sort() + len() 

In this, we perform task of sorting using sort() and call external function as the key to solve problem of counting elements with next element greater. The size is computed using len().

Python3




# Python3 code to demonstrate working of
# Sort Matrix by Next Greater Frequency
# Using sort() + len()
 
 
# getting frequency of next greater
def get_greater_freq(row):
 
    # getting length
    return len([row[idx] for idx in range(0, len(row) - 1) if row[idx] < row[idx + 1]])
 
 
# initializing list
test_list = [[4, 6, 2, 9, 10], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# inplace sorting
test_list.sort(key=get_greater_freq)
 
# printing result
print("Sorted rows : " + str(test_list))


Output:

The original list is : [[4, 6, 2, 9, 10], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]] 
Sorted rows : [[6, 3, 2], [5, 3, 2, 5], [4, 6, 2, 9, 10], [2, 4, 5, 6, 7, 7]] 

Time Complexity: O(nlogn+mlogm)
Auxiliary Space: O(1)

Method #2 : Using sorted() + len() + lambda

In this, we perform task of sorting using sorted(), lambda and len() are used for creating one-liner functionality to perform sorting o the basis of number of elements greater than their previous element.

Python3




# Python3 code to demonstrate working of
# Sort Matrix by Next Greater Frequency
# Using sorted() + len() + lambda
 
# initializing list
test_list = [[4, 6, 2, 9, 10], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# performing one-liner sorting
# avoiding external fnc. call
res = sorted(test_list, key=lambda row: len(
    [row[idx] for idx in range(0, len(row) - 1) if row[idx] < row[idx + 1]]))
 
# printing result
print("Sorted rows : " + str(res))


Output:

The original list is : [[4, 6, 2, 9, 10], [5, 3, 2, 5], [2, 4, 5, 6, 7, 7], [6, 3, 2]] 
Sorted rows : [[6, 3, 2], [5, 3, 2, 5], [4, 6, 2, 9, 10], [2, 4, 5, 6, 7, 7]] 

Time Complexity: O(n*logn), where n is the length of the input list. This is because we’re using the built-in sorted() function which has a time complexity of O(nlogn) in the worst case.
Auxiliary Space: O(n), as we’re using additional space other than the input list itself. 

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12064 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7220 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS