<?php
function bin_search( $search, $array, $low=0, $high=null, $search = 0 ) {
if( $search == count( $array ) )
return null;
if( ! is_array( $array ) || empty( $array ) )
return null;
if( empty( $high ) )
$high = count( $array ) - 1;
// not found
if( $low === $high)
return null;
$mid = floor( ( $high - $low ) / 2 );
$key = ( $high - $mid ) - 1;
// found!
if( $array[ $key ] === $search )
return $key;
if( $search < $array[ $key ] ) {
return bin_search( $search, $array, $low, $key, ++$search );
} else {
return bin_search( $search, $array, $key, $high, ++$search );
}
}
$array = range(1,5);
$key = bin_search( 3, $array );
echo "Key=" . $key;
echo "Value=" . $array[ $key ];
- Output for 7.0.0 - 7.0.1
- Fatal error: Redefinition of parameter $search in /in/SlG11 on line 3
Process exited with code 255. - Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28
- Key=0Value=1
preferences:
175.32 ms | 1395 KiB | 36 Q