Skip to content

Instantly share code, notes, and snippets.

@xquery
Created March 15, 2015 16:40
Show Gist options
  • Save xquery/c66bec2d3695e9026fae to your computer and use it in GitHub Desktop.
Save xquery/c66bec2d3695e9026fae to your computer and use it in GitHub Desktop.
binary search in xquery
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