3v4l.org

run code in 300+ PHP versions simultaneously
<?php echo "Practicing Binary Search <br>"; function printArray($arr){ foreach ($arr as $value) { echo $value.", "; # code... } echo "<br>"; } $list=array(3,6,5,7,13,10,1,8); //--------------------------------------------------------------------- //function echo "Binary Search, recursive method <br>"; function BSearch($num,$list){ sort($list); printArray($list); echo "<br>"; $left=0; $right=count($list)-1; echo "Number to search:".$num."<br>"; $key=floor(recursiveBSearch($num,$list,$left,$right)); echo "The key is ".$key; } function recursiveBSearch($num,$arr,$left,$right){ if($left>$right){ return -1; } $mid=($left+$right)/2; if($arr[$mid]==$num){ return $mid; } else if($arr[$mid]<$num){ return recursiveBSearch($num,$arr,$mid+1,$right); } else if($arr[$mid]>$num){ return recursiveBSearch($num,$arr,$left,$mid-1); } } //----------------------------------------------------------------------------- //calling function BSearch(5,$list); ?>
Output for 8.1.0 - 8.1.29, 8.2.0 - 8.2.21, 8.3.0 - 8.3.9
Practicing Binary Search <br>Binary Search, recursive method <br>1, 3, 5, 6, 7, 8, 10, 13, <br><br>Number to search:5<br> Deprecated: Implicit conversion from float 3.5 to int loses precision in /in/h6i6p on line 40 Deprecated: Implicit conversion from float 3.5 to int loses precision in /in/h6i6p on line 43 Deprecated: Implicit conversion from float 3.5 to int loses precision in /in/h6i6p on line 46 Deprecated: Implicit conversion from float 1.25 to int loses precision in /in/h6i6p on line 40 Deprecated: Implicit conversion from float 1.25 to int loses precision in /in/h6i6p on line 43 Deprecated: Implicit conversion from float 2.375 to int loses precision in /in/h6i6p on line 40 The key is 2
Output for 5.6.0 - 5.6.38, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
Practicing Binary Search <br>Binary Search, recursive method <br>1, 3, 5, 6, 7, 8, 10, 13, <br><br>Number to search:5<br>The key is 2

preferences:
242.82 ms | 404 KiB | 290 Q