🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
Sorting a list in descending order is a common operation in Python programming. It can help in data analysis where you may need to organize data from highest to lowest. While Python's built-in functions provide a straightforward approach, understanding how to achieve this manually deepens comprehension of sorting algorithms.
Definition
Sorting a list in descending order means arranging the list's elements from largest to smallest. This can be done with the built-in sort() method by setting the reverse parameter to True, or by using a custom sorting algorithm like bubble sort in reverse.
2. Program Steps
1. Define an unsorted list.
2. Implement a sorting algorithm, or use the sort() method with reverse order.
3. Print the list sorted in descending order.
3. Code Program
# Step 1: Define an unsorted list
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Step 2: Use the built-in sort() method to sort the list in-place in descending order
numbers.sort(reverse=True)
# Step 3: Print the sorted list
print(f"List sorted in descending order: {numbers}")
# Alternative manual method without using sort()
# Implementing bubble sort in reverse
for i in range(len(numbers)):
for j in range(0, len(numbers) - i - 1):
if numbers[j] < numbers[j + 1]: # Change the comparison operator to sort in descending order
numbers[j], numbers[j + 1] = numbers[j + 1], numbers[j]
# Step 3: Print the list sorted in descending order manually
print(f"List sorted in descending order (manually): {numbers}")
Output:
List sorted in descending order: [9, 5, 5, 4, 3, 2, 1, 1] List sorted in descending order (manually): [9, 5, 5, 4, 3, 2, 1, 1]
Explanation:
1. numbers starts as an unsorted list of integers.
2. The sort() method is called with reverse=True, which sorts numbers in descending order in-place.
3. The first print statement displays numbers sorted in descending order using the sort() method.
4. In the alternative method, a nested loop structure implements the bubble sort algorithm, but with the comparison operator in the if condition changed to < so that larger numbers move to the front of the list.
5. The second print statement shows numbers after manually sorting them in descending order. Both outputs match, demonstrating two ways to achieve the same result.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment