Skip to content

Instantly share code, notes, and snippets.

@vbauerster
Created April 23, 2014 20:35
Show Gist options
  • Save vbauerster/11231448 to your computer and use it in GitHub Desktop.
Save vbauerster/11231448 to your computer and use it in GitHub Desktop.
bsearch
var bsearch = function(arr, k){
var l = 0, r = arr.length-1, m;
console.log(arr);
while(l<r){
m = (l+r)/2^0;
console.log(m);
if(k>arr[m])
l=m+1;
else
r=m;
}
if(k===arr[r])
return r;
else
return -1;
}
// console.log((5)/2^0);
var arr = [10,20,30,40,50,60,70,80];
console.log("index: " + bsearch(arr, 40));
console.log("index: " +
bsearch(arr.sort(function(a,b){return b-a}), 50));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment