Binary search: Difference between revisions

→‎{{header|Quackery}}: improved flattening a nest from O(n^2) to O(log n)
(Added Quackery)
(→‎{{header|Quackery}}: improved flattening a nest from O(n^2) to O(log n))
Line 5,497:
 
=={{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
Line 5,510:
value.bs share > iff
[ 1 - unrot nip ]
else again
[ 1+ nip ] again ]
again ]
drop
valuenest.bs releasetake over peek
value.bs take =
nest.bsdup releasedip ][ not + ] ] is binarysearch ( [ n --> n b )
 
[ dup echo
2dup binarysearch iff
[ say " was identified atas item " ]
tuck 2swap peek
rot = iff
[ say " was identified at" ]
else
[ say " cancould bego stuffedinto position in" ]
echo say "." cr ] is task ( [ n --> n )</lang>
1+ ]
say " position "
echo say "." cr ] is task ( [ n --> n )</lang>
 
{{out}}
 
Testing in the 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 atas positionitem 2.
66 cancould bego stuffed ininto position 6.
 
Stack empty.</pre>
1,462

edits