GithubHelp home page GithubHelp logo

binary-search's Introduction

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.

The 3rd and 4th arguments to the comparator are the current index and array, respectively. You shouldn't normally need the index or array to compare values, but it's there if you do.

You may also, optionally, specify an input range as the final two parameters, in case you want to limit the search to a particular range of inputs. However, be advised that this is generally a bad idea (but sometimes bad ideas are necessary).

License

To the extent possible by law, The Dark Sky Company, LLC has waived all copyright and related or neighboring rights to this library.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.