Python | Check for Descending Sorted List
Last Updated :
09 Apr, 2023
The sorted operation of list is essential operation in many application. But it takes best of O(nlogn) time complexity, hence one hopes to avoid this. So, to check if this is required or not, knowing if list is by default reverse sorted or not, one can check if list is sorted or not. Lets discuss various ways this can be achieved.
Method #1 : Naive method The simplest way to check this is run a loop for first element and check if we could find any larger element than it after that element, if yes, the list is not reverse sorted.
Python3
# Python3 code to demonstrate
# Check for Descending Sorted List
# using naive method
# initializing list
test_list = [10, 8, 4, 3, 1]
# printing original list
print ("Original list : " + str(test_list))
# using naive method to
# Check for Descending Sorted List
flag = 0
i = 1
while i < len(test_list):
if(test_list[i] > test_list[i - 1]):
flag = 1
i += 1
# printing result
if (not flag) :
print ("Yes, List is reverse sorted.")
else :
print ("No, List is not reverse sorted.")
Output : Original list : [10, 8, 4, 3, 1]
Yes, List is reverse sorted.
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) additional space is not needed
Method #2 : Using sort() + reverse The new list can be made as a copy of the original list, sorting the new list and comparing with the old list will give us the result if sorting was required to get reverse sorted list or not.
Python3
# Python3 code to demonstrate
# Check for Descending Sorted List
# using sort() + reverse
# initializing list
test_list = [10, 5, 4, 3, 1]
# printing original list
print ("Original list : " + str(test_list))
# using sort() + reverse to
# Check for Descending Sorted List
flag = 0
test_list1 = test_list[:]
test_list1.sort(reverse = True)
if (test_list1 == test_list):
flag = 1
# printing result
if (flag) :
print ("Yes, List is reverse sorted.")
else :
print ("No, List is not reverse sorted.")
Output : Original list : [10, 8, 4, 3, 1]
Yes, List is reverse sorted.
Time Complexity: O(nlogn), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1), constant extra space is required
Method #3 : Using all() function
The all() function returns True if all elements of the iterable are True. Hence, by comparing each element with its next, we can find if the list is reverse sorted or not.
Python3
# Python3 code to demonstrate
# Check for Descending Sorted List
# using all() function
# initializing list
test_list = [10, 8, 4, 3, 1]
# printing original list
print ("Original list : " + str(test_list))
# using all() function to
# Check for Descending Sorted List
if all(test_list[i] >= test_list[i+1] for i in range(len(test_list)-1)):
print ("Yes, List is reverse sorted.")
else:
print ("No, List is not reverse sorted.")
#This code is contributed by Edula Vinay Kumar Reddy
OutputOriginal list : [10, 8, 4, 3, 1]
Yes, List is reverse sorted.
Time complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python - Test for strictly decreasing list The test for a monotonic sequence is a utility that has manifold applications in mathematics and hence every sphere related to mathematics. As mathematics and Computer Science generally go parallel, mathematical operations such as checking for strictly decreasing sequences can be useful to gather kn
5 min read
Check if List is Strictly Increasing - Python We are given a list of numbers and our task is to check whether the list is strictly increasing meaning each element must be greater than the previous one. For example: a = [1, 3, 5, 7] this is strictly increasing and b = [1, 3, 3, 7], this is not strictly increasing as 3 appears twice.Using all() w
2 min read
Sort Nested Dictionary by Value Python Descending Sorting a nested dictionary in Python based on its values is a common task, and it becomes even more versatile when you need to sort in descending order. In this article, we'll explore some different methods to achieve this using various Python functionalities. Let's dive into more ways to efficient
3 min read
Sort a list in python Sorting is a fundamental operation in programming, allowing you to arrange data in a specific order. Here is a code snippet to give you an idea about sorting.Python# Initializing a list a = [5, 1, 5, 6] # Sort modifies the given list a.sort() print(a) b = [5, 2, 9, 6] # Sorted does not modify the gi
5 min read
Sort List of Lists Ascending and then Descending in Python Sorting a list of lists in Python can be done in many ways, we will explore the methods to achieve the same in this article.Using sorted() with a keysorted() function is a flexible and efficient way to sort lists. It creates a new list while leaving the original unchanged.Pythona = [[1, 2, 3], [3, 2
3 min read
Python - Descending Sort String Numbers Reverse Sorting a list is easy task and has been dealt with in many situations. With Machine Learning and Data Science emerging, sometimes we can get the data in the format of list of numbers but with string as data type. Generic Sort functions give erroneous result in that case, hence several other
2 min read