Skip to content

Commit 5d6213f

Browse files
committed
Add an example for sorter in dpnp.searchsorted docstrings
1 parent 63ce858 commit 5d6213f

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

dpnp/dpnp_iface_searching.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,14 @@ def searchsorted(a, v, side="left", sorter=None):
316316
If ``"left"``, the index of the first suitable location found is given.
317317
If ``"right"``, return the last such index. If there is no suitable
318318
index, return either 0 or N (where N is the length of `a`).
319+
319320
Default: ``"left"``.
320321
sorter : {dpnp.ndarray, usm_ndarray}, optional
321322
Optional 1-D array of integer indices that sort array a into ascending
322-
order. They are typically the result of :obj:`dpnp.argsort`.
323+
order. They are typically the result of :py:func:`dpnp.argsort`.
323324
Out of bound index values of `sorter` array are treated using
324325
``"wrap"`` mode documented in :py:func:`dpnp.take`.
326+
325327
Default: ``None``.
326328
327329
Returns
@@ -338,7 +340,7 @@ def searchsorted(a, v, side="left", sorter=None):
338340
Examples
339341
--------
340342
>>> import dpnp as np
341-
>>> a = np.array([11,12,13,14,15])
343+
>>> a = np.array([11, 12, 13, 14, 15])
342344
>>> np.searchsorted(a, 13)
343345
array(2)
344346
>>> np.searchsorted(a, 13, side='right')
@@ -347,6 +349,19 @@ def searchsorted(a, v, side="left", sorter=None):
347349
>>> np.searchsorted(a, v)
348350
array([0, 5, 1, 2])
349351
352+
When `sorter` is used, the returned indices refer to the sorted
353+
array of `a` and not `a` itself:
354+
355+
>>> a = np.array([40, 10, 20, 30])
356+
>>> sorter = np.argsort(a)
357+
>>> sorter
358+
array([1, 2, 3, 0]) # Indices that would sort the array 'a'
359+
>>> result = np.searchsorted(a, 25, sorter=sorter)
360+
>>> result
361+
array(2)
362+
>>> a[sorter[result]]
363+
array(30) # The element at index 2 of the sorted array is 30
364+
350365
"""
351366

352367
usm_a = dpnp.get_usm_ndarray(a)

0 commit comments

Comments
 (0)