This article mainly and we introduce the PHP implementation of the binary search algorithm, combined with specific examples of the PHP binary search algorithm implementation and use of skills, involving PHP array judgment, traversal, calculation and other related operations, the need for friends can refer to, hope to help everyone.
<?php$arr = Array (4,58,11,34,88,45,32,54,63,78), function binary ($arr, $bnum) {if (Is_array ($arr) && count ($ ARR) > 0) {sort ($arr); $start = 0; $end = count ($arr)-1; $mid =-1; while ($start <= $end) { $mid = floor ($sta rt+ $end)/2); if ($arr [$mid] = = $bnum) { return $arr [$mid]; } else if ($arr [$mid] < $bnum) { $start = $mid +1; } else if ($arr [$mid] > $bnum) { $end = $mid-1;}} return-1;}} $mm = Binary ($arr, +);p Rint_r ($MM);//Output 32?>
Related recommendations:
An example analysis of the binary search algorithm of PHP implementation
Using PHP to implement binary search algorithm code sharing
Using PHP to implement binary search algorithm code sharing _php Tips