## 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
{"ft_lang_label":"__label__en","ft_lang_prob":0.7175067,"math_prob":0.90406454,"size":7726,"snap":"2022-40-2023-06","text_gpt3_token_len":1868,"char_repetition_ratio":0.15578866,"word_repetition_ratio":0.029366305,"special_character_ratio":0.23401502,"punctuation_ratio":0.06823529,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9863778,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-09-30T12:31:01Z\",\"WARC-Record-ID\":\"<urn:uuid:cc3ab454-805f-42a1-9e1c-8c9549387125>\",\"Content-Length\":\"70771\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:30530fd4-6c65-4e1f-8bf6-3b43e39f1e06>\",\"WARC-Concurrent-To\":\"<urn:uuid:bedda1d4-39ae-435f-892f-c6f6366d0240>\",\"WARC-IP-Address\":\"166.62.6.102\",\"WARC-Target-URI\":\"https://tutorials.ducatindia.com/numpy-argsort/\",\"WARC-Payload-Digest\":\"sha1:KEQSSTLELIRSQF6AQ2LGQFSUKP7FMLPS\",\"WARC-Block-Digest\":\"sha1:TSLHNK56Z2OLC4CYSZM66FNRNN2GQLUW\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030335469.40_warc_CC-MAIN-20220930113830-20220930143830-00346.warc.gz\"}"}