Python program that uses the sort()
method and the reverse
argument to sort the elements of an array in descending order:
def sort_descending(arr):
arr.sort(reverse=True)
return arr
# Example usage
arr = [5, 3, 8, 4, 1, 9, 6]
print(sort_descending(arr))
The function sort_descending(arr)
takes an array as an argument. It uses the sort()
method with the reverse
argument set to True
to sort the elements of the array in descending order. The function returns the sorted array.
The example usage creates an array and calls the sort_descending()
function to sort the elements of the array in descending order, and then it prints the sorted array.
Alternatively, you could use the sorted()
function to sort the elements of an array in descending order by passing the reverse=True
argument to it.
def sort_descending(arr):
return sorted(arr,reverse=True)
This method uses the sorted()
function to sort the elements of the array in descending order by passing the reverse=True
argument to it and returns the sorted array.
You could also use the numpy.sort()
function to sort the elements of an array in descending order by passing kind='quicksort', order='descending'
arguments to it
import numpy as np
def sort_descending(arr):
return np.sort(arr,kind='quicksort', order='descending')
This method uses the numpy.sort()
function to sort the elements of the array in descending order by passing kind='quicksort', order='descending'
arguments to it and returns the sorted array.