Sorting Arrays in Python: Arranging Elements in Order

thumb_up 1  ·  sell Python array sorting, Sorting elements in Python arrays, Arranging array items in Python

Python's array module defines the array class. An object of array class is similar to the array as present in Java or C/C++. Unlike the built-in Python sequences, array is a homogenous collection of either strings, or integers, or float objects.

The array class doesn't have any function/method to give a sorted arrangement of its elements. However, we can achieve it with one of the following approaches −

  • Using a sorting algorithm

  • Using the sort() method from List

  • Using the built-in sorted() function

Let's discuss each of these methods in detail.

Using a Sorting Algorithm

We shall implement the classical bubble sort algorithm to obtain the sorted array. To do it, we use two nested loops and swap the elements for rearranging in sorted order.

Save the following code using a Python code editor −

 
import array as arr a = arr.array('i', [10,5,15,4,6,20,9]) for i in range(0, len(a)): for j in range(i+1, len(a)): if(a[i] > a[j]): temp = a[i]; a[i] = a[j]; a[j] = temp; print (a)

It will produce the following output −

array('i', [4, 5, 6, 9, 10, 15, 20])

Using the sort() Method from List

Even though array doesn't have a sort() method, Python's built-in List class does have a sort method. We shall use it in the next example.

First, declare an array and obtain a list object from it, using tolist() method −

a = arr.array('i', [10,5,15,4,6,20,9]) b=a.tolist()

We can easily obtain the sorted list as follows −

b.sort()

All we need to do is to convert this list back to an array object −

a.fromlist(b)

Here is the complete code −

from array import array as arr a = arr.array('i', [10,5,15,4,6,20,9]) b=a.tolist() b.sort() a = arr.array('i') a.fromlist(b) print (a)

It will produce the following output −

array('i', [4, 5, 6, 9, 10, 15, 20])

Using the Builtin sorted() Function

The third technique to sort an array is with the sorted() function, which is a built-in function.

The syntax of sorted() function is as follows −

sorted(iterable, reverse=False)

The function returns a new list containing all items from the iterable in ascending order. Set reverse parameter to True to get a descending order of items.

The sorted() function can be used along with any iterable. Python array is an iterable as it is an indexed collection. Hence, an array can be used as a parameter to sorted() function.

from array import array as arr a = arr.array('i', [4, 5, 6, 9, 10, 15, 20]) sorted(a) print (a)

It will produce the following output −

array('i', [4, 5, 6, 9, 10, 15, 20])


The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back