NumPy Sort, Search and Counting Functions - NumPy

What are the different sorting functions provided by NumPy?

NumPy provides different sorting functions which are used for implementing sorting algorithms which are classified in accordance with either speed of the execution, performance and the workspace required and the algorithm stability. The sorting algorithms are compared as follows:

kind
speed
worst case
work space
stable
‘quicksort’
1
O(n^2)
0
no
‘mergesort’
2
O(n*log(n))
~n/2
yes
‘heapsort’
3
O(n*log(n))
0
no

numpy.sort()

A copy of the input array is returned by this function. The parameters of the function are:

Where,

S.No
Parameter & Description
1.
a
Array to be sorted
2.
axis
The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis
3.
kind
Default is quicksort
4.
order
If the array contains fields, the order of fields to be sorted

Example

The output appears as:

numpy.argsort()

The indirect sor is performed by this function by using a specific sort type and the array of the indices of the data is returned by this function. This indices array enables to construct the sort array.

Example

The output appears as:

numpy.lexsort()

The indirect sorting is retuned by this function by using a sequence of keys. The key appears as the spreadsheet columns. An array of indices is returned by this function.

Example

The output appears as:

To search inside n array, NumPy facilitates a variety of functions. The functions also include finding the maximum, minimum and the element satisfying a condition.

numpy.argmax() and numpy.argmin()

The maximum and the minimum elements are returned by these functions.

Example

The output appears as:

numpy.nonzero()

The indices of the non-zero elements is returned by this function.

Example

The output appears as:

numpy.where()

The indices of the elements in the input array satisfying a given condition is returned by this function.

Example

The output appears as:

numpy.extract()

The element satisfying a particular condition is returned by this function.

Example

The output appears as:

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

NumPy Topics