Binary search: Difference between revisions

Content added Content deleted
No edit summary
No edit summary
Line 2,148: Line 2,148:
6 found at index 4
6 found at index 4
250 not found</pre>
250 not found</pre>

=={{header|Lambdatalk}}==
<lang scheme>
{def BS
{def BS.r {lambda {:a :v :i0 :i1}
{let { {:a :a} {:v :v} {:i0 :i0} {:i1 :i1}
{:m {floor {* {+ :i0 :i1} 0.5}}} }
{if {< :i1 :i0}
then :v is not found
else {if {> {array.item :a :m} :v}
then {BS.r :a :v :i0 {- :m 1} }
else {if {< {array.item :a :m} :v}
then {BS.r :a :v {+ :m 1} :i1 }
else :v is at array[:m] }}}}} }
{lambda {:a :v}
{BS.r :a :v 0 {- {array.length :a} 1}} }}
-> BS

{def A {array 12 14 16 18 20 22 25 27 30}}
-> A = [12,14,16,18,20,22,25,27,30]

{BS {A} -1} -> -1 is not found
{BS {A} 24} -> 24 is not found
{BS {A} 25} -> 25 is at array[6]
{BS {A} 123} -> 123 is not found

{def B {array {serie 1 100000 2}}}
-> B = [1,3,5,... 99997,99999]

{BS {B} 100} -> 100 is not found
{BS {B} 12345} -> 12345 is at array[6172]
</lang>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==