Python Program Sort Elements In Array Descending Order
# Initialize an empty array
array_of_elements = []
# Get the number of elements you want in the array from the user
elements = int(input("Enter the number of elements you want in the array: "))
# Loop to take user input and add numbers to the array
for i in range(elements):
# Get the element from the user
user_input = int(input(f"Enter element {i + 1}: "))
# Add the number to the array
array_of_elements.append(user_input)
# Sorting the array in descending order using sorted() with reverse=True
sorted_element_descending = sorted(array_of_elements, reverse=True)
# Displaying the sorted array in descending order
print("Original array:", array_of_elements)
print("Sorted array (descending):", sorted_element_descending)
Output:
Enter the number of elements you want in the array: 5
Enter element 1: 44
Enter element 2: 23
Enter element 3: 54
Enter element 4: 16
Enter element 5: 2
Original array: [44, 23, 54, 16, 2]
Sorted array (descending): [54, 44, 23, 16, 2]