Numpy searchsorted() Function



The Numpy searchsorted() function is used to find indices where elements should be inserted to maintain order. It works on sorted arrays and can handle one-dimensional arrays efficiently. This function is particularly useful for tasks like merging sorted arrays or finding insertion points for new elements.

The numpy.searchsorted() function uses a binary search algorithm to efficiently determine the indices at which elements should be inserted to maintain the order of a sorted array. If the input array is not sorted, the sorter parameter can be used to specify an array of indices that sort the input array.

Syntax

Following is the syntax of the Numpy searchsorted() function −

numpy.searchsorted(a, v, side='left', sorter=None)

Parameters

Following are the parameters of the Numpy searchsorted() function −

  • a: The input array which should be sorted.
  • v: The scalar or array-like values that to be inserted into an array
  • sorter(optional): If the input array is not sorted, this parameter is used to sort.
  • side(optional): This specifies which index to return if aa value exists −
  • 'left'(default): Returns the first suitable index.
  • 'right': Returns the last suitable index.

Return Type

This function returns an array of insertion points where elements of v should be inserted into a to maintain order.

Example

Following is an basic example to find insertion points in a sorted array using Numpy searchsorted() function −

import numpy as np

sorted_array = np.array([1, 3, 5, 7])
values = [2, 4, 6]
insert_indices = np.searchsorted(sorted_array, values)

print("Sorted Array:", sorted_array)
print("Values to Insert:", values)
print("Insert Indices:", insert_indices)

Output

Sorted Array: [1 3 5 7]
Values to Insert: [2, 4, 6]
Insert Indices: [1 2 3]

Example: Using side Parameter

The side parameter determines the index when the value exists in the array.

Here, we are inserting a value, 5, which already exists in the sorted_array. When the side parameter is set to left, the function returns the first suitable index for insertion, which is 2. When the side parameter is set to right, the function returns the last suitable index for insertion, which is 4

import numpy as np

sorted_array = np.array([1, 3, 5, 5, 7])
value = 5

left_index = np.searchsorted(sorted_array, value, side='left')
right_index = np.searchsorted(sorted_array, value, side='right')

print("Sorted Array:", sorted_array)
print("Value to Insert:", value)
print("Left Index:", left_index)
print("Right Index:", right_index)

Output

Sorted Array: [1 3 5 5 7]
Value to Insert: 5
Left Index: 2
Right Index: 4

Example: With sorter Parameter

If the input array is not sorted, we can provide a sorter parameter that represents indices for sorting.

In the following example, we have inserted values into an unsorted array by providing sorter parameter in the numy.searchsoreted() function −

import numpy as np

unsorted_array = np.array([40, 10, 30, 20])
sorter = np.argsort(unsorted_array)
values = [25, 15]
insert_indices = np.searchsorted(unsorted_array, values, sorter=sorter)

print("Unsorted Array:", unsorted_array)
print("Sorter Indices:", sorter)
print("Values to Insert:", values)
print("Insert Indices:", insert_indices)

Output

Unsorted Array: [40 10 30 20]
Sorter Indices: [1 3 2 0]
Values to Insert: [25, 15]
Insert Indices: [2 1]

Example: Scalar quantities as a Parameter

When a scalar is passed as v, the result is a single integer instead of an array.

Here, we have passed a value, 25, into a sorted_array using the numpy.searchsorted() function, which returned a single integer indicating the index where the value should be inserted to maintain the array's sorted order −

import numpy as np

sorted_array = np.array([10, 20, 30, 40])
value = 25
insert_index = np.searchsorted(sorted_array, value)

print("Sorted Array:", sorted_array)
print("Value to Insert:", value)
print("Insert Index:", insert_index)

Output

Sorted Array: [10 20 30 40]
Value to Insert: 25
Insert Index: 2
numpy_array_manipulation.htm
Advertisements