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)
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 :
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 :
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]]
- Python | Merge Python key values to list
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- Any & All in Python
- try and except in Python
- SQL using Python | Set 1
- try-except vs If in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.