0
0
NumPydata~10 mins

np.searchsorted() for insertion points in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - np.searchsorted() for insertion points
Start with sorted array
Choose value to insert
np.searchsorted() finds index
Return insertion index
Use index to insert value without breaking order
np.searchsorted() finds the position where a value should be inserted in a sorted array to keep it sorted.
Execution Sample
NumPy
import numpy as np
arr = np.array([10, 20, 30, 40])
idx = np.searchsorted(arr, 25)
print(idx)
Finds the index where 25 should be inserted in the sorted array [10,20,30,40].
Execution Table
StepArrayValue to InsertSearch DirectionIndex FoundExplanation
1[10, 20, 30, 40]25left (default)225 fits between 20 (index 1) and 30 (index 2)
2[10, 20, 30, 40]5left (default)05 is less than 10, insert at start
3[10, 20, 30, 40]40left (default)340 equals element at index 3, insert before it
4[10, 20, 30, 40]40right4With side='right', insert after existing 40
5[10, 20, 30, 40]50left (default)450 is greater than all, insert at end
💡 All insert positions found to keep array sorted.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
arr[10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40][10, 20, 30, 40]
valueN/A255404050
sideleft (default)leftleftleftrightleft
indexN/A20344
Key Moments - 3 Insights
Why does np.searchsorted return 3 for value 40 with default side='left'?
Because side='left' means insert before existing equal values. Since 40 is at index 3, insertion index is 3 (see execution_table row 3).
What changes when side='right' is used for value 40?
With side='right', insertion happens after existing equal values. So for 40, index is 4 (see execution_table row 4).
Why is the insertion index 0 for value 5?
Because 5 is smaller than all elements, it should be inserted at the start (index 0), keeping array sorted (see execution_table row 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table row 1. What index does np.searchsorted return for inserting 25?
A1
B2
C3
D0
💡 Hint
Check the 'Index Found' column in row 1 of execution_table.
At which step does np.searchsorted return the insertion index at the end of the array?
AStep 5
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the largest index equal to array length in 'Index Found' column.
If side='right' is used for value 40, what insertion index is returned?
A3
B2
C4
D1
💡 Hint
Check execution_table row 4 for side='right' insertion index.
Concept Snapshot
np.searchsorted(sorted_array, value, side='left')
- Finds index to insert value in sorted_array
- side='left' inserts before equal values
- side='right' inserts after equal values
- Keeps array sorted after insertion
- Returns integer index for insertion
Full Transcript
np.searchsorted() helps find where to insert a value in a sorted array so the order stays correct. It takes a sorted array and a value to insert. By default, it finds the leftmost place to insert the value, meaning before any equal values. You can change this with the side parameter to 'right' to insert after equal values. The function returns the index where the value should go. For example, in [10, 20, 30, 40], inserting 25 returns index 2 because 25 fits between 20 and 30. Inserting 40 with side='left' returns 3, before the existing 40. With side='right', it returns 4, after the existing 40. This helps keep arrays sorted when adding new elements.