Skip to content

[Term Entry] NumPy Ndarray: .searchsorted() #7354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions content/numpy/concepts/ndarray/terms/searchsorted/searchsorted.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
---
Title: 'searchsorted()'
Description: 'Returns the index where a value should be inserted to maintain order.'
Subjects:
- 'Computer Science'
- 'Data Science'
- 'Machine Learning'
Tags:
- 'Arrays'
- 'Binary Search'
- 'NumPy'
- 'Search'
- 'Sorting Algorithms'
CatalogContent:
- 'learn-python-3'
- 'paths/computer-science'
---

**`.searchsorted()`** in a NumPy `ndarray` returns the index where a value should be inserted to maintain order.

## Syntax

```pseudo
ndarray.searchsorted(value, side='left', sorter=None)
```

**Parameters:**

- `value` (array_like): Values to insert into the array.
- `side` (default=`'left'`, optional): Determines whether to return the first suitable location (`'left'`) or last (`'right'`).
- `sorter` (1-D array_like, optional): Specifies a pre-sorted index array for the search (used if the array isn’t sorted).

**Return value:**

The index (or indices) where `values` should be inserted to maintain order.

## Example

In this example, `.searchsorted()` finds the index where the value `3` should be inserted to keep the array sorted:

```py
import numpy as np

arr = np.array([1, 2, 3, 4, 5])
index = arr.searchsorted(3)
print(index)
```

The result is `2`, which is the position of the first `3` in the sorted array.

## Codebyte Example

In this example, `.searchsorted()` finds where a new test score should be inserted in a sorted list of scores:

```codebyte/python
import numpy as np

scores = np.array([55, 60, 65, 70, 75])
new_score = 68
position = scores.searchsorted(new_score)
print(position)
```