
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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