Binary search: Difference between revisions

m
(Add bruijn)
 
(2 intermediate revisions by one other user not shown)
Line 2,521:
 
=={{header|Bruijn}}==
As defined in <code>std/Combinator</code>:
<syntaxhighlight lang="bruijn">
:import std/NumberCombinator .
:import std/Math .
:import std/List .
:import std/Option .
 
binary-search [y [[[[[2 <? 3 none go]]]]] (+0) --(∀0) 0]
# sage bird combinator
y go [[1compare-case eq lt gt (02 !! 0)] [1] (03 0+ 2)]]
eq some 0
lt 5 4 --0 2 1
gt 5 ++0 3 2 1
 
# example using sorted list of x^3, x=[-50,50]
# factorial using y
factorial yfind [[=?0map-or (+1)"not (found" [0 : (1 !! 0)] (binary--search 0) 1)] lst]
lst take (+100) ((\pow (+3)) <$> (iterate ++‣ (-50)))
 
:test ((factorialfind (+6100)) =? (+720))"not ([[1]]found")
:test ((fibonaccihead (find (+6125))) =? (+855)) ([[1]])
 
:test ((head (find (+117649))) =? (+99)) ([[1]])
# (very slow) fibonacci using y
fibonacci y [[0 <? (+1) (+0) (0 <? (+2) (+1) rec)]]
rec (1 --0) + (1 --(--0))
 
:test ((fibonacci (+6)) =? (+8)) ([[1]])
</syntaxhighlight>
 
Line 2,889 ⟶ 2,891:
 
'''iterative''' -- almost a direct translation of the pseudocode
<syntaxhighlight lang="chapel">proc binsearch(A : [], value) {
{
var low = A.domain.dim(1).low;
var highlow = A.domain.dim(10).highlow;
whilevar (lowhigh <= highA.domain.dim(0) {.high;
while (low <= high)
{
var mid = (low + high) / 2;
 
Line 2,909 ⟶ 2,913:
{{out}}
4
 
=={{header|Clojure}}==
'''Recursive'''
6

edits