微信公众号定制开发

二分查找
<pre class="prettyprint lang-js"><a href="https://github.com/ningbnii/suanfatujie" target="_blank">https://github.com/ningbnii/suanfatujie</a> </pre> <pre class="prettyprint lang-js">function binary_search($list, $item) { $low = 0; $high = count($list) - 1; while ($low &lt;= $high) { $mid = ceil(($low + $high) / 2); $guess = $list[$mid]; if ($guess == $item) { return $mid; } elseif ($guess &gt; $item) { $high = $mid - 1; } else { $low = $mid + 1; } } return false; } $myList = [1, 3, 5, 7, 9]; echo binary_search($myList, 5);</pre>

上一篇:没有更多文章了

下一篇:选择排序的php实现