0
0
NumPydata~10 mins

np.argsort() for sort indices in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.argsort() for sort indices
Input Array
Compute Indices that Sort
Return Sorted Indices Array
Use Indices to Access Sorted Elements
np.argsort() takes an array and returns the indices that would sort the array. These indices can then be used to reorder the original array.
Execution Sample
NumPy
import numpy as np
arr = np.array([30, 10, 20])
indices = np.argsort(arr)
sorted_arr = arr[indices]
This code finds the indices that sort the array and then uses them to get the sorted array.
Execution Table
StepActionArray StateIndices ComputedResult
1Start with array[30, 10, 20]N/AN/A
2Call np.argsort(arr)[30, 10, 20][1, 2, 0]Indices that sort array
3Use indices to reorder array[30, 10, 20][1, 2, 0][10, 20, 30]
4End[30, 10, 20][1, 2, 0]Sorted array obtained
💡 All steps completed; indices correctly represent sorting order
Variable Tracker
VariableStartAfter np.argsortAfter indexingFinal
arr[30, 10, 20][30, 10, 20][30, 10, 20][30, 10, 20]
indicesN/A[1, 2, 0][1, 2, 0][1, 2, 0]
sorted_arrN/AN/A[10, 20, 30][10, 20, 30]
Key Moments - 3 Insights
Why does np.argsort return indices instead of the sorted array?
np.argsort returns indices to show the order to rearrange the original array without changing it. This is shown in execution_table step 2, where indices [1, 2, 0] tell us how to reorder arr.
How do we get the sorted array from the indices?
By using the indices to index the original array, as in execution_table step 3, arr[indices] gives the sorted array [10, 20, 30].
What if the array has duplicate values?
np.argsort will return indices that sort duplicates in their original order (stable sort). This behavior is consistent with the indices shown in the example.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does the indices array [1, 2, 0] represent?
APositions to rearrange arr to sorted order
BSorted values of arr
COriginal array reversed
DIndices of maximum values
💡 Hint
Check the 'Indices Computed' column at step 2 in execution_table
At which step in the execution_table do we get the sorted array values?
AStep 2
BStep 3
CStep 1
DStep 4
💡 Hint
Look at the 'Result' column for the sorted array values
If the original array was [5, 5, 1], what would np.argsort(arr) return?
A[2, 1, 0]
B[0, 1, 2]
C[2, 0, 1]
D[1, 0, 2]
💡 Hint
Indices should sort the array: smallest value index first, duplicates keep original order
Concept Snapshot
np.argsort(array) returns indices that sort the array.
Use these indices to reorder the original array.
It does not sort the array itself.
Useful for sorting related arrays or stable sorting.
Example: indices = np.argsort(arr); sorted_arr = arr[indices]
Full Transcript
np.argsort() is a function in numpy that returns the indices which would sort an array. Instead of returning the sorted array directly, it gives you the order to rearrange the original array. For example, if you have an array [30, 10, 20], np.argsort returns [1, 2, 0] because the element at index 1 (10) is the smallest, then index 2 (20), then index 0 (30). You can then use these indices to get the sorted array by indexing the original array with them. This method is helpful when you want to sort multiple related arrays in the same order or keep track of the original positions. The execution table shows each step: starting with the array, computing indices, using them to reorder, and obtaining the sorted array. Key points include understanding that argsort returns indices, not sorted values, and how to use those indices to get sorted data.