PHP binary search Algorithm sample sharing

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.