CodesCompiler
🔍
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact 🚀 Get Started Free

PYTHON - Sort Arrays

Python - Sort Arrays

Python’s arraymodule 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 eitherstrings, 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 thesort()method from List
  • Using the built-insorted()function

Let’s discuss each of these methods in detail.

Sort Arrays Using a Sorting Algorithm

We implement the classicalbubble sort algorithmto obtain the sorted array. To do it, we use twonested loopsand swap the elements for rearranging in sorted order.

Example

Run the following code using a Python code editor −

PYTHON CODE
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 followingoutput−

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

Sort Arrays Using sort() Method of List

Even though array module doesn’t have asort()method, Python’s built-inListclass does have a sort method. We shall use it in the next example.

First, declare an array and obtain a list object from it, usingtolist()method. Then, use the sort() method to get a sorted list. Lastly, create another array using the sorted list which will display a sorted array.

Example

The following code shows how to get sorted array using the sort() method.

PYTHON CODE
import array as arr

# creating array
orgnlArray = arr.array('i', [10,5,15,4,6,20,9])
print("Original array:", orgnlArray)
# converting to list 
sortedList = orgnlArray.tolist()
# sorting the list
sortedList.sort()

# creating array from sorted list
sortedArray = arr.array('i', sortedList)
print("Array after sorting:",sortedArray)

The above code will display the following output −

PYTHON CODE
Original array: array('i', [10, 5, 15, 4, 6, 20, 9])
Array after sorting: array('i', [4, 5, 6, 9, 10, 15, 20])

Sort Arrays Using sorted() Method

The third technique to sort an array is with thesorted()function, which is abuilt-in function.

Thesyntaxof sorted() function is as follows −

PYTHON CODE
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.

Thesorted()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.

Example

In this example, we will see the use of sorted() method in sorting an array.

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

It will produce the followingoutput−

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