Signup/Sign In

Python NumPy Sorting

In this tutorial, we will cover the concept of sorting of ndarray elements in the NumPy library.

Sorting is basically a process where elements are arranged in an ordered sequence.

  • The ordered sequence basically is any sequence that has an order in corresponding to the elements. It can be numeric or alphabetical, ascending, or descending, anything.

  • There are many functions for performing sorting, available in the NumPy library. We have various sorting algorithms like quicksort, merge sort and heapsort, and all these are implemented using the numpy.sort() function.

The kind of sorting algorithm that is to be used in the sort operation must be mentioned in the function call.

Various Sorting Algorithms

Various sorting algorithms along with their properties that are used are as given below:

Serial No. Kind Worst case Complexity Speed Stable Work Space
1. 'quicksort' O(n^2) 1 No 0
2. 'heapsort' O(n*log(n)) 3 No 0
3. 'mergesort' O(n*log(n)) 2 Yes -n/2
4. 'timsort' O(n*log(n)) 2 Yes -n/2

NOTE: If you are new to sorting algorithms, you should learn various sorting algorithms first, to get an idea about how they work.

In the above table, stable means that the order occurence of the elements in the input array will be kept same in the output array too.

From NumPy version 1.12.0, quicksort has been changed to introsort.

Now, we will discuss the sort() function of the NumPy Library:

Numpy sort() function

This function will return the sorted copy of the input ndarray in Numpy.

The required syntax to use this function is as follows:

numpy.sort(a, axis, kind=None, order=None)

Parameters:

Now we will discuss the parameters of this function:

  • a
    This parameter will indicate the input array to be sorted

  • axis
    This parameter is used to indicate the axis along which the array needs to be sorted. If the value of this parameter is None, then the array is flattened before sorting. The default value of this parameter is -1, which sorts along the last axis.

  • kind
    This parameter will specify the sorting algorithm. The default value of this parameter is 'quicksort'. Note that both 'stable' and 'mergesort' use timsort or radix sort under the covers and, in general, the actual implementation will vary along with the data type. The 'mergesort' option is retained for backward compatibility.

  • order
    This parameter will represent the fields according to which the array is to be sorted in the case if the array contains the fields.

Returned Values:

This function will return the sorted array of the same type and having the same shape as the input array.

Example 1:

Below we have the code snippet:

import numpy as np 

# sorting along the first axis
a = np.array([[17, 15], [10, 25]]) 
arr1 = np.sort(a, axis = 0)
print ("Sorting Along first axis : \n")
print(arr1)

# sorting along the last axis 
b = np.array([[1, 15], [20, 18]]) 
arr2 = np.sort(b, axis = -1) 
print ("\nSorting along last axis : \n")
print(arr2) 


c = np.array([[12, 15], [10, 1]]) 
arr3 = np.sort(c, axis = None) 
print ("\nSorting Along none axis : \n")
print(arr3) 

sorting NumPy array

Example 2:

Below we have another example where we will sort according to the name:

d = np.dtype([('name', 'S10'),('marks',int)])  
  
arr = np.array([('Mukesh',200),('John',251)],dtype = d)  
  
print("Sorting data ordered by name")  
  
print(np.sort(arr,order = 'name'))  


Sorting data ordered by name
[(b'John', 251) (b'Mukesh', 200)]

Summary

In this tutorial, we hadve covered the concept of sorting in NumPy library. We have also covered numpy.sort() function along with its syntax, parameters, and returned values.



About the author:
Aspiring Software developer working as a content writer. I like computer related subjects like Computer Networks, Operating system, CAO, Database, and I am also learning Python.