while( $low <= $high ) { if ($P[$current] == $t) { return true; } if ($t < $P[$current]) { $high = $current-1; } else { $low = $current+1 } $current = floor($high + $low)/2 } return false;}
<?php function binarySearch($t, $P, $low, $high, $current) { $mid = $low + floor(($high - $low) / 2); // Found current, return mid index + 1 if ($P[$t][$mid] == $current && $mid < count($P[$t]) - 1) { return $P[$t][$mid+1]; // Two choices left, pick high }elseif ($high - $low == 1) { return $P[$t][$high]; // Recursive call on right (higher) half }elseif ($P[$t][$mid] < $current) { return binarySearch($t, $P, $mid+1, $high, $current); // Recursive call on left (lower) half }elseif ($P[$t][$mid] > $current) { return binarySearch($t, $P, $low, $mid, $current); } }
$x = $P[$t]; $mid = ($low+$high)/2; if($low<$high) { if($x[$mid] > $current) { return binarySearch($t, $P, $low, $mid, $current); } else if($x[$mid] < $current) { return binarySearch($t, $P, $mid+1, $high, $current); } } return $x[$mid];}
"cat" => [1,6,7], "dog" => [1,2,23,25,27,50]];
$i = (int) (($low + $high) / 2); while(true){ if ($current<$P[$t][$i]) { if($high == $i) return $P[$t][$i]; $high = $i; $i=(int) (($i+$low)/2); } else if ($P[$t][$i] < $current){ if ($low == $i) return $P[$t][$i+1]; $low = $i; $i=(int) (($i+$high)/2); } }}
if ($high >= $low) { $mid = $low + ($high - $low) / 2; // If an index after $current is present if ($P[$t][$mid] > $current && $P[$t][$mid-1] < $current) return $P[$t][$mid]; // Search only left subarray // if current index is smaller than // the midpoint's index if ($P[$t][$mid] > $current) return binarySearch($t, $P, $low, $mid - 1, $current); // Search only right subarray // if current index is larger than // the midpoint's index return binarySearch($t,$P, $mid + 1, $high, $current); }
// when no index in array satisfying the conditions return -1; }// end of function
$t = "dog"; $P = [ "cat" => [1, 6, 7], "dog" => [1 ,2, 23, 25, 27, 50] ]; echo binarySearch($t, $P, 1, 4, 15)."\n";(Edited: 2018-09-12)
<?php
function binarySearch($t, $p, $low, $high, $current){ // Find the array of the term $arr = $p[$t]; /* Start the while loop till $low is less than equal to $high. */ while($low <= $high){ // Find middle position of the array $avg = floor(($low+$high)/2); // If we find something greater than // equal to $current move down if($arr[$avg]>=$current){ $high = $avg-1; }else{ // Else move up in the array $low = $avg+1; } } // return the value which is equal or // first greater than the $current return $arr[$low];}
$t = "dog"; $P = [ "cat" => [1, 6, 7], "dog" => [1 ,2, 23, 25, 27, 50] ]; echo binarySearch($t, $P, 1, 5, 15);
"cat" =>[1,6,7], "dog"=>[1,2,23,25,27,50]];echo binarySearch($t,$P,0,5,15);
if($high-$low == 1) { return ($P[$t][$high]); } while($low < $high) { $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); }
}}
function binarySearch($t, $P, $low, $high, $current) {
while($high - $low > 1) { $mid = (int)(($low + $high)/2); if(($P[$t][$mid] > $current)) { $high = $mid; } else { $low = $mid; } } if($high - $low == 1) { if($P[$t][$low] <= $current) { if($P[$t][$high] > $current) { return $P[$t][$high]; } } } return -1;}
"cat" => [1, 6, 7], "dog" => [1 ,2, 23, 25, 27, 50]]; echo binarySearch($t, $P, 0, 1, 1); echo "\n";
// check for empty array if (count($P) === 0) return 0; while ($low <= $high) { // compute middle index $mid = floor(($low + $high) / 2); // element found at mid if($P[$t][$mid] >= $current && $P[$t][$mid - 1] < $current) { return $P[$t][$mid]; } if ($current < $P[$t][$mid]) { // search the left side of the array $high = $mid -1; } else { // search the right side of the array $low = $mid + 1; } } // If we reach here the element doesnt exist return 0;}