Created
March 15, 2015 16:40
-
-
Save xquery/c66bec2d3695e9026fae to your computer and use it in GitHub Desktop.
binary search in xquery
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
xquery version "1.0-ml"; | |
declare function local:binary($min,$max,$number){ | |
let $middle := round( (($max - $min) div 2) + $min ) | |
return | |
if($middle eq $number) | |
then $number | |
else | |
if($middle lt $number) | |
then local:binary($middle,$max,$number) | |
else local:binary($min,$middle,$number) | |
}; | |
let $min := 0 | |
let $max := 1000000 | |
let $number := (xdmp:random($max - $min)) | |
return local:binary($min,$max,$number) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment