Skip to content

Utils.Function.binarySearch

Christian d'Heureuse edited this page Sep 25, 2026 · 1 revision

commons-math-interpolation / Utils / binarySearch

Function: binarySearch()

function binarySearch(a, key): number;

Defined in: Utils.ts:176

Searches a sorted array for a value, using the binary search algorithm.

This function corresponds to java.util.Arrays.binarySearch(). Unlike the Java function, it throws an exception when it encounters a NaN value, and it does not distinguish between -0 and +0.

Parameters

a

ArrayLike<number>

The array to be searched. It must be sorted in ascending order, otherwise the result is unspecified. If the array contains multiple elements equal to key, it is unspecified which one is found.

key

number

The value to be searched for.

Returns

number

The index of key, if it is contained in the array. Otherwise -(insertionPoint + 1). The insertion point is the index at which key would be inserted into the array: the index of the first element greater than key, or a.length if all elements are less than key. The return value is >= 0 if and only if key is found.

Throws

Error If key or an array element that is compared with it is NaN.

Clone this wiki locally