/** * Created by PhpStorm. * User: just * Date: 24.11.15 * Time: 11:01 */ function binary($array, $search, $from, $to) { if ($from > $to) { return -1; } $middle = (int) (($from + $to) / 2); if ($search == $array[$middle]) { return $middle; } else { if ($search > $middle) { return binary($array, $search, $middle + 1, $to); } else { return binary($array, $search, $from, $middle - 1); } } }
function decBinary($value, $positions = 8) { return sprintf('%5d', $value) . ': ' . binary($value, $positions); }
/** * Main recursive function */ function binary($array, $search, callable $function, $itr) { $returnData = $function($array, $search); return equals($returnData(), $search) || empty($returnData()) || count($returnData()) == 1 ? equals($returnData(), $search) : binary($returnData(), $search, $function, $itr); }