Skip to content

ts-ds / binarySearch

Function: binarySearch() ​

ts
function binarySearch<T>(
   array, 
   target, 
   compareFn?
): number;

Defined in: search/search.ts:79

Finds the first element equal to target in a sorted array with binary search: compare with the middle element and continue in the half that can contain target.

Time: bestTime: averageTime: worstMemory
O(log n)O(log n)O(log n)O(1)

Type Parameters ​

Type ParameterDescription
T

Parameters ​

ParameterTypeDefault valueDescription
arrayreadonly T[]undefinedArray sorted in the order of compareFn.
targetTundefinedValue to find.
compareFn?CompareFn<T>defaultCompareOrder of the array.

Returns ​

number

Index of the first equal element, or -1.

MIT License