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 |
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 ] |
||
again |
|||
[ 1+ nip ] again ] |
|||
again ] |
|||
drop |
drop |
||
nest.bs take over peek |
|||
value.bs take = |
|||
dup dip [ not + ] ] is binarysearch ( [ n --> n b ) |
|||
[ dup echo |
[ dup echo |
||
binarysearch iff |
|||
⚫ | |||
tuck 2swap peek |
|||
rot = iff |
|||
⚫ | |||
else |
else |
||
[ say " |
[ say " could go into position " ] |
||
⚫ | |||
1+ ] |
|||
say " position " |
|||
⚫ | |||
{{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 |
30 was identified as item 2. |
||
66 |
66 could go into position 6. |
||
Stack empty.</pre> |
Stack empty.</pre> |