Binary search: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
(Added Quackery)
Line 5,495: Line 5,495:
return mid
return mid
return high if abs(l[high] - value) < abs(l[low] - value) else low</lang>
return high if abs(l[high] - value) < abs(l[low] - value) else low</lang>

=={{header|Quackery}}==
Written from pseudocode for rightmost insertion point, iterative. The Quackery word to insert an item into a nest is <code>stuff</code>.

<lang Quackery> [ stack ] is value.bs ( --> n )
[ stack ] is nest.bs ( --> n )

[ value.bs put
dup nest.bs put
size 1 - 0
[ 2dup < if done
2dup + 1 >>
nest.bs share over peek
value.bs share > iff
[ 1 - unrot nip ]
else
[ 1+ nip ]
again ]
drop
value.bs release
nest.bs release ] is binarysearch ( [ n --> n )

[ dup echo
2dup binarysearch
tuck 2swap peek
rot = iff
[ say " was identified at" ]
else
[ say " can be stuffed in"
1+ ]
say " position "
echo say "." cr ] is task ( [ n --> n )</lang>

{{out}}

Testing in shell.

<pre>/O> ' [ 10 20 30 40 50 60 70 80 90 ] 30 task
... ' [ 10 20 30 40 50 60 70 80 90 ] 66 task
...
30 was identified at position 2.
66 can be stuffed in position 6.

Stack empty.</pre>


=={{header|R}}==
=={{header|R}}==