A variety of sorting related functions are available in NumPy. These sorting functions implement different sorting algorithms, each of them characterized by the speed of execution, worst case performance, the workspace required and the stability of algorithms. Following table shows the comparison of three sorting algorithms.

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 |

The sort() function returns a sorted copy of the input array. It has the following parameters −

```
numpy.sort(a, axis, kind, order)
```

Parameter | Description |
---|---|

a |
Array to be sorted |

axis |
The axis along which the array is to be sorted. If none, the array is flattened, sorting on the last axis |

kind |
Default is quicksort |

order |
If the array contains fields, the order of fields to be sorted |

In [1]:

```
import numpy as np
a = np.array([[3,7],[9,1]])
print ('Our array is:')
print (a)
print ('Applying sort() function:')
print (np.sort(a))
print ('Sort along axis 0:')
print (np.sort(a, axis = 0))
# Order parameter in sort function
dt = np.dtype([('name', 'S10'),('age', int)])
a = np.array([("raju",21),("anil",25),("ravi", 17), ("amar",27)], dtype = dt)
print ('Our array is:')
print (a)
print ('Order by name:')
print (np.sort(a, order = 'name'))
```

The numpy.argsort() function performs an indirect sort on input array, along the given axis and using a specified kind of sort to return the array of indices of data. This indices array is used to construct the sorted array.

In [2]:

```
import numpy as np
x = np.array([3, 1, 2])
```

In [4]:

```
print ('Our array is:')
print (x)
```

In [5]:

```
print ('Applying argsort() to x:')
y = (np.argsort(x))
print (y)
```

In [6]:

```
print ('Reconstruct original array in sorted order:')
print (x[y])
```

In [7]:

```
print ('Reconstruct the original array using loop:')
for i in y:
print (x[i])
```

This function performs an indirect sort using a sequence of keys. The keys can be seen as a column in a spreadsheet. The function returns an array of indices, using which the sorted data can be obtained. Note, that the last key happens to be the primary key of sort.

In [8]:

```
import numpy as np
nm = ('raju','anil','ravi','amar')
dv = ('f.y.', 's.y.', 's.y.', 'f.y.')
ind = np.lexsort((dv,nm))
print ('Applying lexsort() function:')
print (ind)
print ('Use this index to get sorted data:')
print ([nm[i] + ", " + dv[i] for i in ind])
```

These two functions return the indices of maximum and minimum elements respectively along the given axis.

In [2]:

```
import numpy as np
a = np.array([[30,40,70],[80,20,10],[50,90,60]])
print ('Our array is:')
print (a)
print ('Applying argmax() function:')
print (np.argmax(a))
print ('Index of maximum number in flattened array')
print (a.flatten())
print ('Array containing indices of maximum along axis 0:')
maxindex = np.argmax(a, axis = 0)
print (maxindex)
print ('Array containing indices of maximum along axis 1:')
maxindex = np.argmax(a, axis = 1)
print (maxindex)
print ('Applying argmin() function:')
minindex = np.argmin(a)
print (minindex)
print ('Flattened array:')
print (a.flatten()[minindex])
print ('Flattened array along axis 0:')
minindex = np.argmin(a, axis = 0)
print (minindex)
print ('Flattened array along axis 1:')
minindex = np.argmin(a, axis = 1)
print (minindex)
```

The numpy.nonzero() function returns the indices of non-zero elements in the input array.

In [3]:

```
import numpy as np
a = np.array([[30,40,0],[0,20,10],[50,0,60]])
print ('Our array is:')
print (a)
print ('\n')
print ('Applying nonzero() function:')
print (np.nonzero (a))
```

The where() function returns the indices of elements in an input array where the given condition is satisfied.

In [6]:

```
import numpy as np
x = np.arange(9.).reshape(3, 3)
print ('Our array is:')
print (x)
print ('\n')
print ('Indices of elements > 3')
y = np.where(x > 3)
print (y)
print ('\n')
print ('Use these indices to get elements satisfying the condition')
print (x[y])
print ('\n')
```

The extract() function returns the elements satisfying any condition.

In [8]:

```
import numpy as np
x = np.arange(9.).reshape(3, 3)
print ('Our array is:')
print (x)
print ('\n')
# define a condition
condition = np.mod(x,2) == 0
print ('Element-wise value of condition')
print (condition)
print ('\n')
print ('Extract elements using condition')
print (np.extract(condition, x))
print ('\n')
```

**Thank You** for visiting our website. If you like our work, please support us so that we can keep on creating new tutorials/blogs on interesting topics (like AI, ML, Data Science, Python, Digital Marketing, SEO, etc.) that can help people learn new things faster. You can support us by clicking on the **Coffee** button at the bottom right corner. We would appreciate even if you can give a thumbs-up to our article in the comments section below.

If you want to

- provide some suggestions on topic
- share your views
- include some details in tutorial
- suggest some new topics on which we should create tutorials/blogs