Binary search: Difference between revisions

Content added Content deleted
(→‎[[Java]]: Added recursive implementation.)
(Added description, PHP examples from my version of the page.)
Line 1: Line 1:
{{task}}
{{task}}
A binary search divides a range of values into halves, and continues to narrow down the field of search until the unknown value is found.
Do a binary search through a sorted integer array for a certain number. Implementations can be recursive or iterative (both if you can). Print out whether or not the number was in the array afterwards. If it was, print the index also. The algorithms are as such (from [http://en.wikipedia.org/wiki/Binary_search the wikipedia]):


As an analogy, consider the children's game "guess a number." The host has a secret number, and will only tell the player if their guessed number is higher than, lower than, or equal to the secret number. The player then uses this information to guess a new number.
Recursive:

As the player, one normally would start by choosing the range's midpoint as the guess, and then asking whether the guess was higher, lower, or equal to the secret number. If the guess was too high, one would select the point exactly between the range midpoint and the beginning of the range. If the original guess was too low, one would ask about the point exactly between the range midpoint and the end of the range. This process repeats until one has reached the secret number.

This is an example of a binary search.

=Task=

Given the starting point of a range, the ending point of a range, and the "secret value", implement a binary search through a sorted integer array for a certain number. Implementations can be recursive or iterative (both if you can). Print out whether or not the number was in the array afterwards. If it was, print the index also. The algorithms are as such (from [http://en.wikipedia.org/wiki/Binary_search the wikipedia]):

=Examples=
==Pseudocode==
===Recursive===
BinarySearch(A[0..N-1], value, low, high) {
BinarySearch(A[0..N-1], value, low, high) {
if (high < low)
if (high < low)
Line 15: Line 27:
}
}


Iterative:
==Iterative==
BinarySearch(A[0..N-1], value) {
BinarySearch(A[0..N-1], value) {
low = 0
low = 0
Line 34: Line 46:
[[Category:Java]]
[[Category:Java]]


==Iterative==
===Iterative===
...
...
//check will be the number we are looking for
//check will be the number we are looking for
Line 56: Line 68:
...
...


==Recursive==
===Recursive===
public static int binarySearch(int[] nums, int check, int lo, int hi){
public static int binarySearch(int[] nums, int check, int lo, int hi){
if(hi < lo){
if(hi < lo){
Line 69: Line 81:
return guess;
return guess;
}
}

==PHP==
===Iterative===
This approach uses a loop.
<pre>
function binary_search( $secret, $start, $end )
{
do
{
$guess = $start + ( ( $end - $start ) / 2 );

if ( $guess > $secret )
$end = $guess;

if ( $guess < $secret )
$start = $guess;

} while ( $guess != $secret );

return $guess;
}
</pre>
===Recursive===
This approach uses recursion.
<pre>
function binary_search( $secret, $start, $end )
{
$guess = $start + ( ( $end - $start ) / 2 );

if ( $guess > $secret )
return (binary_search( $secret, $start, $guess ));

if ( $guess < $secret )
return (binary_search( $secret, $guess, $end ) );

return $guess;
}
</pre>