×

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.


Related Topics

Python Program to find the length of a string

Python program to find the length of a string A string in Python is a set of Unicode characters. It can't be changed once it's been declared. The number of characters...

2 minutes read.

Python Goto Statement

We all know that Python is the most basic and widely used programming language in the world. It is also one of the world's most popular and widely used languages....

4 minutes read.

How to upgrade PIP in python

We use the PIP command in our command prompt to install any package. PIP is used to install published Python packages in the PyPI (Python package index) or other indices....

3 minutes read.

Python String isalnum() method

Python String isalnum() method The string.isalnum () method in Python returns a boolean value true if all characters in the string are alphanumeric else for any other value it returns false. Syntax String.isalnum() Parameter NA Return This...

1 minute read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Convert List to Array in Python

What is List in Python? In Python, a list is a built-in data structure that stores a collection of items. The items in a list can be of any data type,...

3 minutes read.

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal

Python Program to Convert Decimal into Binary, Octal, and Hexadecimal We know that the most widely used number system is a decimal system, but the computer only understands binary values. The...

2 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

What is Sleeping Time in Python

Did you ever postpone a Python program's execution? Usually, you want your code to execute as quickly as possible. But there are times when it is in your best interests...

3 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python List

The list is one of the most versatile, mutable data-structures in Python. It can store heterogeneous data or different types of data. The list contains comma-separated values (item) within the square brackets. Creating...

4 minutes read.

Python Seaborn

Seaborn is an open-source library in Python that is used for data visualization and plotting graphs. The plots are used for the Visualization of data. It is built on top...

10 minutes read.

Python Loop through a Dictionary

Introduction in this tutorial, we will discuss in python How to Loop Through a Dictionary. In contrast to other Data Types, which can only retain a single value as an element, a Dictionary...

4 minutes read.

Expressions in Python

What is Expression in Python? The expression contains more than one operator as well as the operands with it. Expression helps us to produce some other values. In the Python programming...

12 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.

To Do GUI Application using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

3 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Python program to find factorial of a given number

Python program to find factorial of a given number Factorial is a non-negative integer. It is the product of all positive integers from 1 to the number we are going to...

3 minutes read.