Program to sort the elements of an Array in ascending order in Python. The program contains array initialisation, for loop iteration statements nested and print statements. This program is for Beginners, class 11,12 and B Tech students.

### Program to sort the elements of an Array in ascending order

### 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.

### How we are going to achieve Array sorting in ascending order ?

We need to sort the given array in ascending order such that elements will be arranged from smallest to largest. 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.

Elements will be sort in such a way that smallest element will appear on extreme left which in this case is 1. The largest element will appear on extreme right which in this case is 8.

### 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], end=" ");
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 ascending order: ");
for i in range(0, len(arr)):
print(arr[i], end=" ");
```

### 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], end=” “);**step 3:Sort the array in ascending 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 the array after sorting**

print(“Elements of array sorted in ascending order: “);

for i in range(0, len(arr)):

print(arr[i], end=” “);

### Output:

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

### Output Explanation:

RESTART: C:/csstudy/source code file 2/Program to sort the elements of an Array in ascending order in Python.py

Elements of original array: Elements of the initialised array is shown here.

5 2 8 7 1

Elements of array sorted in ascending order: After the calculation and placing of the element it is shown here.

1 2 5 7 8

### Conclusion:

The array is running fine.

Thank you for reading, learning and download the source code of Program to sort the elements of an Array in ascending order in Python. The program contains array initialisation, for loop iteration statements nested and print statements. This program is for Beginners, class 11,12 and B Tech computer science students. Below link of more programs, class 11,12 practical file and project file upto B Tech level is given :

/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