Binary search: Difference between revisions

Content added Content deleted
(Added Quackery)
(→‎{{header|Quackery}}: improved flattening a nest from O(n^2) to O(log n))
Line 5,497: Line 5,497:


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


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


[ value.bs put
[ value.bs put
Line 5,510: Line 5,510:
value.bs share > iff
value.bs share > iff
[ 1 - unrot nip ]
[ 1 - unrot nip ]
else
again
[ 1+ nip ]
[ 1+ nip ] again ]
again ]
drop
drop
value.bs release
nest.bs take over peek
value.bs take =
nest.bs release ] is binarysearch ( [ n --> n )
dup dip [ not + ] ] is binarysearch ( [ n --> n b )


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


{{out}}
{{out}}


Testing in shell.
Testing in the shell.


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


Stack empty.</pre>
Stack empty.</pre>