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: best | Time: average | Time: worst | Memory |
|---|---|---|---|
| O(log n) | O(log n) | O(log n) | O(1) |
Type Parameters
| Type Parameter | Description |
|---|---|
T |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
array | readonly T[] | undefined | Array sorted in the order of compareFn. |
target | T | undefined | Value to find. |
compareFn? | CompareFn<T> | defaultCompare | Order of the array. |
Returns
number
Index of the first equal element, or -1.