# Python program to sort the array element into ascending order

Python program to sort the array element into ascending order

In this example, we'll see how to sort the array elements in ascending order using a Python program. Sorting is a method where the smallest element appears on the leftmost side of an array and the largest element appearing on the rightmost side of an array. We can achieve this by using two loops in our program. The outer loops will select an element, and inner loops will compare the selected element with the rest of the element of an array and swap the elements upon the given condition in loops. After the complete iteration, we'll get the sorted array in ascending order.

Example:

Input: arr1 = [6, 8, 9, 2, 4]

Output: arr1 = [2, 4, 6, 8, 9]

Algorithm:

• Declare and initialize an array.
• Iterates through the array and selects an element.
• Inner loops will compare the selected element with the rest of the element of an array.
• If any element is less than the selected element, swap both elements.
• Iterates this process until each element of an array is sorted.
```#Python program to sort the elements of an array in ascending order
#Initialize an array
arr1 = [2, 7, 8, 9, 10, 22, 25]
#Declare a variable
temp = 0
#Print element of an original array
print("The element of an original array is: ")
for i in arr1:  print(i)
#now sort the array
for i in range(0, len(arr1)):
for j in range(i+1, len(arr1)):
if arr1[i] > arr1[j]:
temp = arr1[i]
arr1[i] = arr1[j]
arr1[j] = temp
print("\r")
#Print sorted array
print("The element of an array sorted in ascending order:")
for i in arr1:   print(i)  ```

Output

Explanation: After each iteration, it will compare the iterated elements with the next element from the second position to the last element of an array. If the first element is greater than the next compared element, then the value will be swapped, and the temp value gets updated. This process will continue till all the elements are sorted in ascending order.