Open In App

numpy.argsort() in Python

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

numpy.argsort() is a function in NumPy that returns the indices that would sort an array. In other words, it gives you the indices that you would use to reorder the elements in an array to be in sorted order.

Example:

Python
import numpy as geek

a = geek.array([2, 0, 1, 5, 4, 1, 9])
print("Input unsorted array : ", a)

b = geek.argsort(a)
print("Output sorted array indices : ", b)
print("Output sorted array : ", a[b])

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]

Explanation: A 1D NumPy array is created and displayed. np.argsort(a) returns the indices that would sort the array. The sorted array is obtained by indexing the original array a with the sorted indices.

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.

Examples of numpy.argsort()

Example 1: Sorting Indices Along Axes Using argsort()

This code demonstrates how to use numpy.argsort() with different sorting algorithms (mergesort and heapsort) to find the indices that would sort a 2D NumPy array along specified axes.

Python
import numpy as geek

a = geek.array([[2, 0, 1], [5, 4, 3]])
print("Input array : ", a)

b = geek.argsort(a, kind='mergesort', axis=0)
print("Output sorted array indices along axis 0: ", b)

c = geek.argsort(a, kind='heapsort', axis=1)
print("Output sorteded array indices along axis 1: ", c)

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

Explanation:

  • Input Array: A 2D NumPy array is created and displayed.
  • Sorting along axis 0 (mergesort): np.argsort(a, kind=’mergesort’, axis=0) sorts the array along the columns (axis 0) using the mergesort algorithm and returns the indices of the sorted elements.
  • Sorting along axis 1 (heapsort): np.argsort(a, kind=’heapsort’, axis=1) sorts the array along the rows (axis 1) using the heapsort algorithm and returns the indices of the sorted elements

Example 2: Extracting Top Two Largest Elements Using numpy.argsort()

This code demonstrates how to use numpy.argsort() to find the indices of the two largest elements in a NumPy array and retrieve them in descending order.

Python
import numpy as np

x=np.array([12,43,2,100,54,5,68])

print(np.argsort(x))
print(np.argsort(x)[-2:])
print(np.argsort(x)[-2:][::-1])
print(x[np.argsort(x)[-2:][::-1]])

Output
[2 5 0 1 4 6 3]
[6 3]
[3 6]
[100  68]

Explanation:

  • np.argsort(x) returns the indices that would sort the array.
  • np.argsort(x)[-2:] selects the indices of the two largest elements.
  • Reversing the indices with [::-1] places the largest element first.
  • Finally, the array is indexed with the reversed indices to extract the two largest elements.


Next Article
Practice Tags :

Similar Reads