 # numpy.argsort() in Python

## numpy.argsort() in Python

`numpy.argsort() `function is used to perform an indirect sort along the given axis using the algorithm specified by the kind keyword. It returns an array of indices of the same shape as arr that that would sort the array.

Syntax : numpy.argsort(arr, axis=-1, kind=’quicksort’, order=None)

Parameters :
arr : [array_like] Input array.
axis : [int or None] Axis along which to sort. If None, the array is flattened before sorting. The default is -1, which sorts along the last axis.
kind : [‘quicksort’, ‘mergesort’, ‘heapsort’]Selection algorithm. Default is ‘quicksort’.
order : [str or list of str] When arr is an array with fields defined, this argument specifies which fields to compare first, second, etc.

Return : [index_array, ndarray] Array of indices that sort arr along the specified axis.If arr is one-dimensional then arr[index_array] returns a sorted arr.

Code #1 :

 `# Python program explaining` `# argpartition() function` `  ` `import` `numpy as geek` ` ` `# input array` `in_arr ``=` `geek.array([ ``2``, ``0``,  ``1``, ``5``, ``4``, ``1``, ``9``])` `print` `(``"Input unsorted array : "``, in_arr) ` ` ` `out_arr ``=` `geek.argsort(in_arr)` `print` `(``"Output sorted array indices : "``, out_arr)` `print``(``"Output sorted array : "``, in_arr[out_arr])`

Output:

```Input unsorted array :  [2 0 1 5 4 1 9]
Output sorted array indices :  [1 2 5 0 4 3 6]
Output sorted array :  [0 1 1 2 4 5 9]
```

Code #2 :

 `# Python program explaining` `# argpartition() function` ` ` `import` `numpy as geek` ` ` `# input 2d array` `in_arr ``=` `geek.array([[ ``2``, ``0``, ``1``], [ ``5``, ``4``, ``3``]])` `print` `(``"Input array : "``, in_arr) ` ` ` `# output sorted array indices` `out_arr1 ``=` `geek.argsort(in_arr, kind ``=``'mergesort'``, axis ``=` `0``)` `print` `(``"Output sorteded array indices along axis 0: "``, out_arr1)` `out_arr2 ``=` `geek.argsort(in_arr, kind ``=``'heapsort'``, axis ``=` `1``)` `print` `(``"Output sorteded array indices along axis 1: "``, out_arr2)`

Output:

```Input array :  [[2 0 1]
[5 4 3]]
Output sorteded array indices along axis 0:  [[0 0 0]
[1 1 1]]
Output sorteded array indices along axis 1:  [[1 2 0]
[2 1 0]]```

Last Updated on March 1, 2022 by admin

## Conway’s Game Of Life (Python Implementation)Conway’s Game Of Life (Python Implementation)

Conway’s Game Of Life (Python Implementation) Conways’s Game Of Life is a Cellular Automation Method

## Python Program to convert List of Integer to List of StringPython Program to convert List of Integer to List of String

Python Program to convert List of Integer to List of String Given a List of

## Oracle Database Connection in PythonOracle Database Connection in Python

Oracle Database Connection in Python Sometimes as part of programming, we required to work with

## Python program to input a comma separated stringPython program to input a comma separated string

Python program to input a comma separated string Given an input string that is comma-separated

## How to save a Python Dictionary to a CSV File?How to save a Python Dictionary to a CSV File?

How to save a Python Dictionary to a CSV File? CSV (comma-separated values) files are one