## Numpy.argsort()

argsort function is a pre-built function present in the Numpy which works in a way that it returns the indices that would be responsible for sorting an array. The array which is returned is arranged in a specified order. The NumPy argsort() function is also used to do a sort which is indirect in nature along the specifies axis (at time the when axis is not specified the default is executed) using a set of algorithms. This algorithm is stipulated by a keyword i.e., ‘kind’. This aids in a way that the resultant array is maintaining the exact shape as the array which was entered maintaining the data that are sorted along the axis.

## Syntax

Following is the representation in which code has to be drafted in the Python language for the application of the numpy argsort function:

argsort(axis=-1,kind=None,order=None)

## Parameter for Argsort()

These are the parameters used in numpy.argsort() have been listed below:

Parameters for Argsort Application of Argsort Parameter
The variable a: array_like The parameter ‘a’ is used to define the array that the user wants to be sorted
axis: int (integer) or None (not defined) (this parameter is optional) The parameter ‘axis’ is used to define the axis alongside which the arranging has to be performed. When nothing is specified for the axis parameter, the default axis taken to be -1. If the axis is set as None, the array which is flattened is processed for performing the sort.
kind: {‘quicksort’, ‘stable’,’heapsort’,’merge * sort’}( this parameter is optional) The parameter ‘kind’ is very essential. It is used to define the algorithm to be sorted. The default parameter for kind is set to “quick sort” unless the user defines another kind. The time sort is used by both stable I and mergesort on the background, but the difference and variety of the data types used of the input array defines their implementation.
order: str (string) or*list of str (this parameter is optional) For an array ‘a’ which has defined fields, the order argument plays the role of defining which field has to be compared first and so on. As all fields do not have be necessarily specified, a single field itself is specified in form of a string. But in such cases, the unspecified fields present would still be used by the function which are used by the d | type and aid in breaking the ties in order related functionalities

Returned argument on execution of the argsort() function:

• index_array:(nd array, int)

The argsort function is utilized to return and the indices representative of the array where the array ‘a’ is sorted along the axis which has been specified by the user.

## Sorting the Algorithms for NumPy

Following are the properties of the three variables with respect to the arguments for argsort():

Kind of argument Pace worst case of computation Occupancy Stability
Quicksort * 1 O * (* n * ^ * *2 *) * 0 * Unstable *
Mergesort * 2 O * (* n * * * log * (n) *) * ~n * / * 2 * Stable *
Heapsort * 3 O * (* n * * * log * (n) *) * 0 * Unstable *

## Example

Let us take some examples to understand n1.argsort() on various dimensionally oriented arrays to understanding the mechanism of sorting used:

## Code:

```import numpy as n1
num=n1.array([564, 11, 321])
n1
b1=n1.argsort(num)
b1
```

## Explanation

an array ‘num’ as been created using the function n1 * . * array() which contains the integer values of 564, 11 and 321 whose values are then sorted using the function. In the output we can see an nd_array has been visualized which is shown to be containing the indices that indicate the actual sequential position of each of the element of the array that has been sort along with the dtype.

## Example #1

Sorting a Two-Dimensional (2-D) Array

## Code:

```import numpy as n1
ar1 = n1.array([[0, 2], [5, 4]])
i1 = n1.argsort(ar1, axis=0)
i1
```

## Explanation

This variable ‘ar1’ is used in the above code to assign and store the value of the 2D array, defining its axis is used for assigning as well as sorting the values after the array has been assigned to store the value of the functional execution of sorting done along its first axis by the ar1.argsort function

## Example #2

FOR an n-Dimensional Array

### Code:

```import numpy as AP
OA=AP.array([300, 100, 200])
print('The elements in our array are :')
print (OA)
print('Using the argsort() function to the array:')
NA = AP.argsort(OA)
print (NA)
print('Sorting our array in ascending order:')
print (OA[NA])
print('Using a loop for reconstruction of our array:')
for x in NA:
print (OA[x]),
```

Apply now for Advanced Python Training Course