Advertisement
rg443

Array.prototype.binaryIndexOf

Jun 12th, 2013
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*  binary search of an array
  2.     Performs a binary search on the host array. This method can either be
  3.     injected into Array.prototype or called with a specified scope like this:
  4.     binaryIndexOf.call(someArray, searchElement);
  5.     http://jsperf.com/binaryindexof-and-indexof/3 */
  6.    */
  7.  
  8. function binaryIndexOf(e){for(var b=0,c=this.length-1,a,d;b<=c;)if(a=(b+c)/2|0,d=this[a],d<e)b=a+1;else if(d>e)c=a-1;else return a;return-1}
  9. Array.prototype.binaryIndexOf=binaryIndexOf;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement