Giter Site home page Giter Site logo

invisioblack / js-binarysearch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from amgadfahmi/js-binarysearch

0.0 1.0 0.0 172 KB

Binary search algorithm implementation in Javascript without any dependency

Home Page: http://amgadfahmi.github.io/js-binarysearch

License: MIT License

JavaScript 88.42% HTML 11.58%

js-binarysearch's Introduction

JS-BinarySearch

Master 1.0.2: Build Status Coverage Status

Binary search algorithm implementation in Javascript without any dependency.

alt text

NPM    bower

Installation

You can install the package using NPM

$ npm install --save-dev js-binarysearch

Using bower

$ bower install --save js-binarysearch

Usage

The liberary can be used to search any numeric, string and object sorted array

var arrNum = [];
for (var i = 0; i < 10000; i++) {
    arrNum.push(i + 1);
}
var result = new BS(arrNum).search(9999);
console.log(result) //output is 9999

Node example

var BS = require("js-binarysearch");
var result = new BS([22,3,55,987,67]).sort().search(55);
console.log(result) //output is 55

Comparison with Linear search

Knowing when to use any of both algorithms is important, as its known that binary search is faster in small array size. The strength of binary search comes with huge data. I have created a small test page to show the execution difference between binary vs linear.

Please not this is assuming its a sorted array and the item we are looking for is the last one.

alt text

Download the project and double click the dist/test.html 
  1. The page let you control the size of the array (iterations = array size)
  2. Every refresh click will perform 2 search commands (linear & binary) and keep the execution time in global array in order to display it later on the chart.
  3. The loop button will perform a complete test (As refresh button was clicked) every 3 seconds (click one more time to stop the loop)
  4. By default the test runs againts numeric, string and object arrays. If you want to perform only one of these or combination, check/unckeck the boxes.
  5. You can click on the chart different legend items to show/hide lines.

Perform test by yourself from here

API

The liberary can be used to search array of numbers, strings and objects. All what is needed to take new instance from the BS class passing the array you want to search in the constructor. The search function has 2 overloads:

  1. The target value in case of numeric and string arrays
  2. Value and key in case of the object as following:
new BS(arrObj).search('20380a36-8777-43f7-a79e-65bdb53f4621', 'guid');

Sorting

Now you can sort the array in chaining mechanism.

new BS([3,1,66,33,890,24]).sort().search(66); //output is 66 
//or 
var arr = [{id:33, name:'Jasmin'},{id:12, name:'David'},{id:7, name:'Amgad'},{id:56, name:'Katya'}];
new BS(arr).sort('id').search(7,'id'); //output is {id:7, name:'Amgad'}
new BS(arr).sort('name').search('Katya','name'); //output is {id:56, name:'Katya'}

License

The MIT License (MIT)

Copyright (c) 2016 Amgad Fahmi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

js-binarysearch's People

Contributors

amgadfahmi avatar

Watchers

 avatar

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.