Home >>Python Programs >Python program to sort an array in descending order
In this example, we will see a Python program through which we can sort the elements of the given array in descending order.
Sorting the elements in descending order means the elements will be arranged from largest to smallest. This can be done through two loops. The outer loop will select an element, and the inner loop will allow us to compare selected elements with the rest of the elements.
#Initialize array
arr = [5, 2, 8, 7, 1, 3, 13, 64, 23, 15, 48, 39, 9];
temp = 0;
#Displaying elements of original array
print("Elements of original array: ");
for i in range(0, len(arr)):
print(arr[i]),
#Sort the array in descending order
for i in range(0, len(arr)):
for j in range(i+1, len(arr)):
if(arr[i] < arr[j]):
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
print();
#Displaying elements of array after sorting
print("Elements of array sorted in descending order: ");
for i in range(0, len(arr)):
print(arr[i]),