Skip to content

ts-ds / traceSearch

Function: traceSearch() ​

ts
function traceSearch<T>(
   algorithm, 
   array, 
   target, 
   compareFn?
): SearchTrace<T>;

Defined in: trace/search.ts:28

Records which elements a search algorithm looks at (probe) and every call of the comparator (compare). Like trace, the algorithm is not changed: the array is wrapped in a Proxy.

Type Parameters ​

Type ParameterDescription
T

Parameters ​

ParameterTypeDescription
algorithm(array, target, compareFn?) => numberSearch function, e.g. binarySearch.
arrayreadonly T[]Array to search.
targetTValue to find.
compareFn?CompareFn<T>Comparator passed to the algorithm.

Returns ​

SearchTrace<T>

The result, the steps and the counters.

Example ​

ts
traceSearch(binarySearch, [1, 3, 5, 7], 5).stats; // { probes: 3, comparisons: 3 }

MIT License