{PHP} Binary Search

<?php
function binary_search() {

$key = rand(1, 10000); $arr = range(1, 10000);

$low = 0; # 最低位 $high = count($arr) - 1; # 最高位 $counter = 0; # 计数器 while ($key != $mid) { $arr_mid = floor(($low + $high) / 2); # 中间数在数组的位置 $mid = $arr[$arr_mid]; # 中间数 $counter++;

<span class="k">if</span> <span class="p">(</span><span class="nv">$key</span> <span class="o">&gt;</span> <span class="nv">$mid</span><span class="p">)</span> <span class="p">{</span>  <span class="c1"># 随机数大于中间数

$low = $arr_mid + 1; } elseif ($key < $mid) { # 随机数小于中间数 $high = $arr_mid -1 ; } }

echo "找到 Array[" . $arr_mid . "] => " . $key . ""; echo "共循环" . $counter . "次"; } ?>

EOF