numpy.searchsorted() function is used to find the indices into a sorted array arr such that, if elements are inserted before the indices, the order of arr would be still preserved. Here, binary search is used to find the required insertion indices.
Syntax : numpy.searchsorted(arr, num, side=’left’, sorter=None)
arr : [array_like] Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it.
num : [array_like]The Values which we want to insert into arr.
side : [‘left’, ‘right’], optional.If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of a).
num : [array_like, Optional] array of integer indices that sort array a into ascending order. They are typically the result of argsort.
Return : [indices], Array of insertion points with the same shape as num.
Code #1 : Working
Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 2
Code #2 :
Input array : [2, 3, 4, 5, 6] The number which we want to insert : 4 Output indices to maintain sorted array : 3
Code #3 :
Input array : [2, 3, 4, 5, 6] The number which we want to insert : [4, 8, 0] Output indices to maintain sorted array : [2 5 0]
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Important differences between Python 2.x and Python 3.x with examples
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Add Logging to Python Libraries
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
- try and except in Python
- Use of min() and max() in Python
- try-except vs If in Python
- Python | a += b is not always a = a + b
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.