binary-search

tiny binary search function with comparators

npm install binary-search
2 downloads in the last day
35 downloads in the last week
184 downloads in the last month

binary-search

This is a really tiny, stupid, simple binary search library for Node.JS. We wrote it because existing solutions were bloated and incorrect.

This version is a straight port of the Java version mentioned by Joshua Bloch in his article, Nearly All Binary Searches and Merge Sorts are Broken.

Thanks to Conrad Irwin and Michael Marino for, ironically, pointing out bugs.

Example

var bs = require("binary-search");
bs([1, 2, 3, 4], 3, function(a, b) { return a - b; }); // => 2
bs([1, 2, 4, 5], 3, function(a, b) { return a - b; }); // => -3

Be advised that passing in a comparator function is required. Since you're probably using one for your sort function anyway, this isn't a big deal.

License

This software is released into the public domain.

npm loves you