# Program to sort the elements of an Array in descending order in python

Program to sort the elements of an Array in descending order in python. The program contains for loop iterative statements, comparison statements, print statement and Array initialisation. This is for Beginners, class 11,12 and B Tech computer science students.

### What is an Array ?

A Python Array is a collection of common type of data structures having elements with same data type. It is used to store collections of data.

### What we will do ?

We need to sort the given array in descending order such that elements will be arranged from largest to smallest. This can be achieved through two loops. The outer loop will select an element, and inner loop allows us to compare selected element with rest of the elements.

### Source code :

``````
arr = [5, 2, 8, 7, 1];
temp = 0;

print("Elements of original array: ");
for i in range(0, len(arr)):
print(arr[i]),

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();

print("Elements of array sorted in descending order: ");
for i in range(0, len(arr)):
print(arr[i]), ``````

### Source code Explanation:

Step 1:Initialize array
arr = [5, 2, 8, 7, 1];
temp = 0;
Step 2:Displaying elements of original array
print(“Elements of original array: “);
for i in range(0, len(arr)):
print(arr[i]),
Step 3: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();
Step 4:Displaying elements of array after sorting
print(“Elements of array sorted in descending order: “);
for i in range(0, len(arr)):
print(arr[i])

### Output :

``` RESTART: C:/csstudy/source code file 2/Program to sort the elements of an Array in descending order in python.py
Elements of original array:
5
2
8
7
1

Elements of array sorted in descending order:
8
7
5
2
1
>>>```

### Output Explanation:

RESTART: C:/csstudy/source code file 2/Program to sort the elements of an Array in descending order in python.py
Elements of original array:
5
2
8
7
1

Elements of array sorted in descending order:
8
7
5
2
1

Step 1 : The initialised array is printed
Step 2: iteration statement and array placing operation is done.
Step 3: Output is print in the descending order.

### Conclusion:

The program is running fine.

Thank you for reading , learning and download the source code of Program to sort the elements of an Array in descending order in python. The program contains for loop iterative statements, comparison statements, print statement and Array initialisation. This is for Beginners, class 11,12 and B Tech computer science students. Below are the links provided for more programs, practical file of class 11, 12 and projects in Python.
/category/python-programs/
Computer Science with Python Practical Book Solutions Class 11 Term -2 – CS Study
Computer Science Practical file for class 12 Term-2 – CS Study
Python Projects for Class 12 with Source Code (Mysql Connectivity) – CS Study