$a = $P[$t]; while($low <= $high) { $m = ($low + $high)/2; if($a[$m] == $current) return $a[$m+1]; if($a[$m] < $current) $low = $m + 1; else $high = $m - 1; } return $a[$low];}
"aadvark" => [1, 5, 9], "frog" => [1, 2, 3, 5, 25, 26, 27, 38, 50], "salamander" => [1 ,3, 14, 67]];
while($high - $low > 1) { $mid = (int)($high+$low) / 2; if($P[$t][$mid] < $current) return binarySearch($t,$P,$mid,$high,$current); else return binarySearch($t,$P,$low,$mid,$current); } if($high - $low == 1) { if($P[$t][$low] <= $current && $P[$t][$high] > $current) return $P[$t][$high]; }}
"aadvark" => [1, 5, 9], "frog" => [1, 2, 3, 5, 25, 26, 27, 38, 50], "salamander" => [1 ,3, 14, 67]]; echo binarySearch($t, $P, 1, 4, 14);
return false; while($low<=$high){ $mid = floor(($high + $low)/2); if ($arr[$mid] == $current) $low=$mid+1; elseif ($arr[$mid] > $current) { $high=$mid-1; } else { $low=$mid+1; } } return $arr[$low];}
"aadvark" => [1, 5, 9], "frog" => [1, 2, 3, 5, 25, 26, 27, 38, 50], "salamander" => [1 ,3, 14, 67]]; echo binarySearch($t, $P, 1, 4, 14);
$mid = $low + ($high-$low)/2; if($low<=$high){ if($P[$t][$low] > $current){ return $P[$t][$low]; } else if($P[$t][$mid] <= $current){ binarySearch($t,$P,$mid + 1, $high, $current); } } return $P[$t][$high];}
"frog" => [1,2,3,5,25,26,2,38,50], "salamander" => [1,3,14,6] ];echo binarySearch($t, $P, 1, 4, 14); ?>
function binarySearch($t, $P, $low, $high, $current){ if($P[$t][$low] > $current){ return $P[$t][$low]; } while($low <= $high){ $mid = ($low + $high)/2; if($P[$t][$mid] == $current){ return $P[$t][$mid + 1]; }elseif($current < $P[$t][$mid]){ $high = $mid - 1; }else{ $low = $mid + 1; } } return $P[$t][floor($high)]; }
"aadvark" => [1, 5, 9], "frog" => [1, 2, 3, 5, 25, 26, 27, 38, 50], "salamander" => [1 ,3, 14, 67] ];echo binarySearch($t, $P, 1, 4, 14);
<?php function binarySearch($t, $P, $low, $high, $current){ if($high>=$low){ $mid=$low + floor(($high-$low)/2); if($P[$t][$mid]==$current && $mid<count($P[$t])){ return $P[$t][$mid]; } else if ($high-$low==1){ return ($P[$t][$high]); } else if ($P[$t][$mid]>$current){ return binarysearch($t,$P,$low,$mid-1,$current); } else if ($P[$t][$mid]<$current){ return binarysearch($t,$P,$mid+1,$high,$current); } } }
$t = "salamander"; $P = [ "aadvark" => [1, 5, 9], "frog" => [1, 2, 3, 5, 25, 26, 27, 38, 50], "salamander" => [1 ,4, 14, 67] ]; echo binarySearch($t, $P, 1,2,9); ?>(Edited: 2019-09-11)
if (count($P) === 0) return false;
$a = $P[$t]; $low = $low - 1;
while ($low <= $high) { $mid = floor(($low + $high) / 2);
if($a[$mid] == $current) { return true; }
if($current < $a[$mid]) { $high = $mid -1; } else { $low = $mid + 1; } } return $a[$low];
$array = $P[$t]; return binarySearchHelper($array, $low, $high, $current);} function binarySearchHelper($arr, $low, $high, $current) {
if($current>$arr[$high]) { return -1; } if($high<$low) { return -1; } if($current < $arr[$low]) { return $arr[$low]; } if($low == $high) { return $arr[high]; } else { $mid = (($high - $low + 1) / 2);
if ($arr[$mid] < $current) return binarySearchHelper($arr, $mid + 1, $high, $current); else return binarySearchHelper($arr, $low, $mid, $current); }} echo(binarySearch($t, $P, $low, $high, $current))
function binarySearch($t, $P, $low, $high, $current) { if($P[$t][$low] > $current){ return $P[$t][$low]; }else if($P[$t][$high] <= $current){ return -1; } while ($low < $high) { $mid = floor(($low + $high) / 2); if($P[$t][$mid] == $current) { return $P[$t][$mid+1]; }elseif ($P[$t][$mid] > $current) { if($P[$t][$mid-1] <= $current){ $high = $mid; }else { $high = $mid - 1; } }else { $low = $mid + 1 ; } } return $P[$t][$high]; }