Binary search: Difference between revisions

→‎{{header|ALGOL 68}}: Added missing ")", use remove formatted transput so can be used with a wider range of compilers.
(Added Algol W)
(→‎{{header|ALGOL 68}}: Added missing ")", use remove formatted transput so can be used with a wider range of compilers.)
Line 540:
 
=={{header|ALGOL 68}}==
<lang algol68>BEGIN
{{works with|ALGOL 68|Revision 1 - no extensions to language used}}
<lang algol68>MODE ELEMENT = STRING;
{{works with|ALGOL 68G|Any - tested with release [http://sourceforge.net/projects/algol68/files/algol68g/algol68g-1.18.0/algol68g-1.18.0-9h.tiny.el5.centos.fc11.i386.rpm/download 1.18.0-9h.tiny]}}
{{wont work with|ELLA ALGOL 68|Any (with appropriate job cards) - tested with release [http://sourceforge.net/projects/algol68/files/algol68toc/algol68toc-1.8.8d/algol68toc-1.8-8d.fc9.i386.rpm/download 1.8-8d] - due to extensive use of FORMATted transput}}
<lang algol68>MODE ELEMENT = STRING;
 
# Iterative: #
PROC iterative binary search = ([]ELEMENT hay stack, ELEMENT needle)INT: (
Line 559 ⟶ 557:
stop iteration:
out
);
 
# Recursive: #
PROC recursive binary search = ([]ELEMENT hay stack, ELEMENT needle)INT: (
Line 579 ⟶ 577:
[]ELEMENT hay stack = ("AA","Maestro","Mario","Master","Mattress","Mister","Mistress","ZZ"),
test cases = ("A","Master","Monk","ZZZ");
 
PROC test search = (PROC([]ELEMENT, ELEMENT)INT search, []ELEMENT test cases)VOID:
FOR case TO UPB test cases DO
Line 585 ⟶ 583:
INT index = search(hay stack, needle);
BOOL found = ( index <= 0 | FALSE | hay stack[index]=needle);
printfprint(($""""g, needle, """ "b, (found|"FOUND at",|"near"), " index "dl$, needlewhole(index, found0), indexnewline))
OD;
test search(iterative binary search, test cases);
test search(recursive binary search, test cases)
)
)END</lang>
Output:
{{out}}
Shows iterative search output - recursive search output is the same.
<pre>
"A" near index 1
3,043

edits