Python program to sort the array element into ascending order

Amansingh Javatpoint
2 min readApr 7, 2021

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.

--

--