@extra-array/bsearch
TypeScript icon, indicating that this package has built-in type declarations

2.9.12 • Public • Published

Binary searches leftmost value in sorted array. 🏃 📼 📦 🌔 📒

Alternatives: bsearch, bsearchRight, bsearchAny, bsearchClosest.
This is part of package extra-array.

array.bsearch(x, v, [fc], [fm]);
// x:  an array (sorted)
// v:  search value
// fc: compare function (a, b)
// fm: map function (v, i, x)
// --> first index of value | ~(index of closest value)
const array = require('extra-array');

var x = [1, 3, 3, 3, 5];
array.bsearch(x, 3);
// 1        ^ found leftmost

array.bsearch(x, 4);
// -5 (~4)           ^ not found, closest

var x = [1, -3, -3, -3, 5];
array.bsearch(x, 3, (a, b) => Math.abs(a) - Math.abs(b));
// 1         ^

array.bsearch(x, 3, null, v => Math.abs(v));
// 1         ^

references

Readme

Keywords

Package Sidebar

Install

npm i @extra-array/bsearch

Weekly Downloads

2

Version

2.9.12

License

MIT

Unpacked Size

11.6 kB

Total Files

24

Last publish

Collaborators

  • wolfram77