Updated July 3, 2023
Definition to NumPy searchsorted
The NumPy searchsorted() function is a built-in function in NumPy package of Python. The NumPy searchsorted() function can be used to find the index value where the new elements should be inserted in the sorted array, and the order of an array be preserved. If the same elements are already present in a sorted array, then we can specify to insert the left or right of it. The NumPy searchsorted() function used the binary search to find the insertion indices.
Syntax:
NumPy.searchsorted( array, values, side = 'left', sorter = None )
Parameters:
- array – This is not an optional parameter, which specifies the array to which value is to be inert.
- values – This is not an optional parameter specifying the values to be inserted in an array.
- side – This is an optional parameter that specifies the side of duplicate value according to which values will be inserted.
- sorter – This is an optional parameter that specifies the optional array of integer indices that sort an array into ascending order.
- Return value – The return value of this function is an insertion index of an array with the same shape as the value.
Working of the NumPy searchsorted() function
The NumPy searchsorted() function accepts four parameters (array, values, side, sorter), and the first two parameters are mandatory parameters. If we pass the array “[11, 22, 33, 44]” as an array and value as 30 to the searchsorted() function, then searchsorted() function returns the inserting index 2 for value 30 because 30 will be inserted after 22 to maintain the sorted order of the array.
Examples of the NumPy searchsorted () function
Example of NumPy searchsorted() function to get the inserting index for value in the array-
Example #1
Code:
# import numpy package as np
import numpy as np
# creating 1d array
array = np.array([10, 20, 30, 40, 50, 60 ])
value = 35
index = np.searchsorted( array, value )
print("The array is : ", array )
print("The value is : ", value )
print( "The output of searchsorted(array, value) function is : " )
print("Index : ", index )
print( "Verify whether the array change after searchsorted() function called : ",array)
print( "\n" )
array = np.array([ 'apple', 'banana', 'mango'])
value = 'cherry'
index = np.searchsorted( array, value )
print("The array is : ", array )
print("The value is : ", value )
print( "The output of searchsorted(array, value) function is : " )
print( "Index : ", index )
print( "Verify whether the array change after searchsorted() function called : ", array)
Output:
Example of NumPy searchsorted() function to get the inserting indices for multiple values in the array with side –
Example #2
Code:
# import numpy package as np
import numpy as np
# creating 1d array
array = np.array([ 10, 20, 30, 35, 40, 50, 60 ])
value = 35
index = np.searchsorted( array, value )
print("The array is : ", array )
print("The value is : ", value )
print( "The output of searchsorted(array, value) function is : ", index )
index = np.searchsorted( array, value, side = 'left')
print( "The output of searchsorted(array, value, side='left') function is : ", index )
index = np.searchsorted( array, value, side = 'rigth')
print( "The output of searchsorted(array, value, side='rigth') function is : ", index )
print( "\n" )
array = np.array([ 'apple', 'banana', 'mango' ])
value = 'mango'
index = np.searchsorted( array, value )
print("The array is : ", array )
print("The value is : ", value )
print( "The output of searchsorted(array, value) function is : ", index )
index = np.searchsorted( array, value, side = 'left')
print( "The output of searchsorted(array, value, side='left') function is : ", index )
index = np.searchsorted( array, value, side = 'rigth')
print( "The output of searchsorted(array, value, side='rigth') function is : ", index )
Output:
It is important to note that 35 is already present in the first sorted array. As a result, the function will return the indices 3, 3, and 4 based on the chosen side. Similarly, to get the inserting index for the value ‘mango,’ which is already present in the second sorted array, the searchsorted() function return 2, 2, 4 indices.
Example to get the inserting index for value in the array with side –
Example #3
Code:
# import numpy package as np
import numpy as np
# creating 1d array
array = np.array([ 10, 20, 30, 35, 40, 50, 60 ])
value = [35, 45]
index = np.searchsorted( array, value )
print("The array is : ", array )
print("The value is : ", value )
print("searchsorted(array, value) function return: ", index )
index = np.searchsorted( array, value, side = 'left')
print("searchsorted(array, value, side='left') function return: ", index )
index = np.searchsorted( array, value, side = 'rigth')
print( "searchsorted(array, value, side='rigth') function return: ", index )
Output:
As in the above program, sorted arrays is created, Next, the searchsorted() function is called with side ‘left’ and ‘right’ to get the inserting index for the multiple values [35, 45], where 35 is already present in sorted array, so the function return [3, 5], [3, 5], [4, 5] indices(where first value is an index for first element and second value is the index for second element)based on the side.
Recommended Articles
This is a guide to NumPy searchsorted. Here we also discuss the definition and Working of the function along with different examples and its code implementation. You may also have a look at the following articles to learn more –