Created
April 23, 2014 20:35
-
-
Save vbauerster/11231448 to your computer and use it in GitHub Desktop.
bsearch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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