Skip to content

ts-ds / exponentialSearch

Function: exponentialSearch() ​

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

Defined in: search/search.ts:103

Finds the first element equal to target in a sorted array: doubles an index (1, 2, 4, …) until it passes target, then runs binary search in the last range. Fast when target is near the beginning and for arrays of unknown length.

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

i is the position of target.

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