🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.searchsorted()

AI & DATA SCIENCE // np-searchsorted

np.searchsorted() finds the index (or indices) at which given values would need to be inserted into an already-sorted array to keep it sorted.

Syntax

np.searchsorted(sorted_arr, values, side='left')

Deep Dive Course

searchsorted() assumes its first argument is already sorted, and uses an efficient binary search, O(log n), rather than a linear scan, to find the insertion point for each value in values — it does not check or enforce that the array is actually sorted, so passing an unsorted array silently produces meaningless results. The side parameter controls tie-breaking when a value already exists in the array: 'left', the default, inserts before any existing equal elements, while 'right' inserts after them.

1Understanding np.searchsorted()

searchsorted() assumes its first argument is already sorted, and uses an efficient binary search, O(log n), rather than a linear scan, to find the insertion point for each value in values — it does not check or enforce that the array is actually sorted, so passing an unsorted array silently produces meaningless results. The side parameter controls tie-breaking when a value already exists in the array: 'left', the default, inserts before any existing equal elements, while 'right' inserts after them.

💡

searchsorted() assumes the input array is already sorted and does not verify this — passing an unsorted array doesn't raise an error, it just silently returns an incorrect, meaningless insertion index.

editor.html
import numpy as np

sorted_arr = np.array([1, 3, 5, 7, 9])
print(np.searchsorted(sorted_arr, 6))
localhost:3000

2Practical Example

Here is a real-world application of np.searchsorted() showing how it is used in production NumPy code.

editor.html
import numpy as np

sorted_arr = np.array([1, 3, 5, 7, 9])
print(np.searchsorted(sorted_arr, [2, 5, 8]))
localhost:3000

3Best Practices

Follow these guidelines when working with np.searchsorted():

1. Only use searchsorted() on data you know is already sorted — sort it explicitly first if you're not certain

2. Use searchsorted() instead of a linear scan when repeatedly finding insertion points in a large sorted array, for its much better O(log n) performance

3. Choose side='left' or side='right' deliberately based on whether you want new values placed before or after existing equal ones

⚠️

Tip: searchsorted() assumes the input array is already sorted and does not verify this — passing an unsorted array doesn't raise an error, it just silently returns an incorrect, meaningless insertion index.

editor.html
import numpy as np

sorted_arr = np.array([1, 3, 5, 7, 9])
print(np.searchsorted(sorted_arr, 6))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

sorted_arr = np.array([1, 3, 5, 7, 9])
print(np.searchsorted(sorted_arr, 6))
Example 02Advanced Example
import numpy as np

sorted_arr = np.array([1, 3, 5, 7, 9])
print(np.searchsorted(sorted_arr, [2, 5, 8]))

Best Practices

  • Only use searchsorted() on data you know is already sorted — sort it explicitly first if you're not certain
  • Use searchsorted() instead of a linear scan when repeatedly finding insertion points in a large sorted array, for its much better O(log n) performance
  • Choose side='left' or side='right' deliberately based on whether you want new values placed before or after existing equal ones

Interview Question

Why is np.searchsorted() much faster than a manual linear scan for finding an insertion point in a large array?

Hint: Think about the algorithm searchsorted() uses internally.

searchsorted() relies on the array already being sorted to perform a binary search, repeatedly checking the middle of the remaining range and discarding half of it based on the comparison, which finds the correct insertion point in O(log n) time. A manual linear scan, by contrast, checks elements one at a time from the start until it finds the right spot, taking O(n) time in the worst case. This difference becomes dramatic for large arrays — searchsorted() on an array of a million elements needs roughly 20 comparisons, while a linear scan could need up to a million.

Exercises

MediumPractice using np.searchsorted() in a real scenario.
View Solution
import numpy as np

sorted_arr = np.array([1, 3, 5, 7, 9])
print(np.searchsorted(sorted_arr, 6))

Frequently Asked Questions

Why is np.searchsorted() much faster than a manual linear scan for finding an insertion point in a large array?

searchsorted() relies on the array already being sorted to perform a binary search, repeatedly checking the middle of the remaining range and discarding half of it based on the comparison, which finds the correct insertion point in O(log n) time. A manual linear scan, by contrast, checks elements one at a time from the start until it finds the right spot, taking O(n) time in the worst case. This difference becomes dramatic for large arrays — searchsorted() on an array of a million elements needs roughly 20 comparisons, while a linear scan could need up to a million.

Related Functions

np-sortnp-argsortsorted()