compareFn: Comparison function defining the element order.
isSorted: Whether the array is pre-sorted (defaults to false).
Returns number
The index of the target in the array, or -1 if not found.
Example
// Basic usage with an array of numbers constdata = [5, 2, 9, 1]; constconfig = { compareFn: (a, b) =>a - b, isSorted:false }; constindex = exponentialSearch(data, 9, config); // index would be 3 after sorting
Example
// Usage with strings in alphabetical order constdata = ['apple', 'banana', 'cherry']; constconfig = { compareFn: (a, b) =>a.localeCompare(b), isSorted:true }; constindex = exponentialSearch(data, 'banana', config); // index would be 1
Performs an exponential search on an array using a custom comparison function.
isSorted
isfalse
, the array will first be sorted using TimSort.