Algorithm Implementation/Search/Binary search

Ada
The following Ada implementation used a generic approach to enable searches on arbitrary data.

Element_Type ; Index_Type  ; Array_Type (Index_Type )  Element_Type; "<"       (Left  :  Element_Type;         Right :  Element_Type) Boolean ;   Search (Elements : Array_Type;      Search   :  Element_Type;      Found    :  Boolean;      Index    :  Index_Type') Left : Index_Type' := Elements'; Right : Index_Type' := Elements' + 1; Search < Elements (Left) Index := Left - 1; Found := False; Center   :  Index_Type   := Left + (Right - Left) / 2; Candidate : Element_Type := Elements (Center); Search = Candidate Index := Center; Found := True; ;               ;                Right - Left <= 1 Index := Center; Found := False; ;               Search < Candidate Right := Center; Left := Center; ;           ;          ;       ;      ;    Search;

C++
The following is a recursive binary search in C++, designed to take advantage of the C++ STL vectors.

A small test suite for the above binary search implementation:

C++ (generic w/ templates)
Here is a more generic iterative binary search using the concept of iterators:

C++ (common Algorithm)
A common binary search Algorithm with its pseudocode:

/* BEGIN BinarySearch(data, size, data2Search) SET low to 0, high to size-1 WHILE low is smaller than or equal to high SET midpoint to (low + high) / 2 IF data2Search is equal to data[midpoint] THEN return midpoint ELSEIF data2Search is smaller than data[midpoint] THEN SET high to midpoint - 1 ELSE SET low to midpoint + 1 ENDIF ENDWHILE RETURN -1 // Target not found END */

C# (common Algorithm)
A common binary search Algorithm:

Java
Binary Search implementation in Java. The algorithm is implemented recursively.

Joshua Bloch wrote the binary search in "java.util.Arrays", so perhaps he knows a thing or two about binary searching in Java.

Test class for a BinarySearch class. Note: Array must be sorted before binary search.

JavaScript
JavaScript binary search algorithm with loop:

JavaScript binary search algorithm implemented on all Array objects using prototype chaining.

Example:

Phix
Note this is available as a builtin

PHP
Working PHP binary search code. Note that this code is extremely inefficient because of the use of array_slice.

Here is a better version from the PHP documentation

Python
Working python binary search code.

That code isn't exactly idiomatic. It's a class, but it has no state. Additionally, it unnecessarily consumes stack space by recursing. Here is another version: (Not binary search actually but binary + linear)

Ruby
Example: