1.0.1 • Published 1 year ago

@namhong2001/binary-search v1.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

very simple binary search

type definition

/**
 * @param low inclusive, must be true on predicate function
 * @param high exclusive,
 * @param predicate predicate function
 */
declare const bsearch: (
  low: number,
  high: number,
  predicate: (mid: number) => boolean
) => number;

Install

npm install @namhong2001/binary-search

example

bsearch(0, 10, (mid) => mid < 5); // 4
bsearch(0, 10, (mid) => mid < 50); // 9, because high(10) is exclusive
bsearch(0, 10, (mid) => mid < -50); // 0, return low(0) even if low is falsy on predicate

LICENSE

MIT