Skip to content

Instantly share code, notes, and snippets.

@zhangchiqing
Created April 23, 2017 04:00
Show Gist options
  • Save zhangchiqing/2723bf98963a82e70147505f0978e45e to your computer and use it in GitHub Desktop.
Save zhangchiqing/2723bf98963a82e70147505f0978e45e to your computer and use it in GitHub Desktop.
simple binary search
var bsearch = function(f, a, b, z) {
if (a + 1 === b) {
return a;
}
var m = Math.floor(( a + b ) / 2);
if (f(m) <= z) {
return bsearch(f, m, b, z);
} else {
return bsearch(f, a, m, z);
}
}
bsearch(R.nth(R.__, [0,4,6,8,9,10,19]), 0, 6, 1) // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment