Created
April 23, 2017 04:00
-
-
Save zhangchiqing/2723bf98963a82e70147505f0978e45e to your computer and use it in GitHub Desktop.
simple binary search
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(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