企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 查找算法 二分查找 - 思路分析:数组中间的值floor((low+top)/2) - 先取数组中间的值floor((low+top)/2)然后通过与所需查找的数字进行比较, - 若比中间值大则将首值替换为中间位置下一个位置,继续第一步的操作; - 若比中间值小,则将尾值替换为中间位置上一个位置,继续第一步操作 - 重复第二步操作直至找出目标数字 ``` <pre class="calibre14">``` <span class="token5">function</span> <span class="token1">BinaryQuery</span><span class="token2">(</span>array $container<span class="token2">,</span> $search<span class="token2">)</span> <span class="token2">{</span> $top <span class="token">=</span> <span class="token1">count</span><span class="token2">(</span>$container<span class="token2">)</span><span class="token2">;</span> $low <span class="token">=</span> <span class="token3">0</span><span class="token2">;</span> <span class="token5">while</span> <span class="token2">(</span>$low <span class="token"><=</span> $top<span class="token2">)</span> <span class="token2">{</span> $mid <span class="token">=</span> <span class="token1">intval</span><span class="token2">(</span><span class="token1">floor</span><span class="token2">(</span><span class="token2">(</span>$low <span class="token">+</span> $top<span class="token2">)</span> <span class="token">/</span> <span class="token3">2</span><span class="token2">)</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">isset</span><span class="token2">(</span>$container<span class="token2">[</span>$mid<span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token4">'没找着哦'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span> <span class="token2">(</span>$container<span class="token2">[</span>$mid<span class="token2">]</span> <span class="token">==</span> $search<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> $mid<span class="token2">;</span> <span class="token2">}</span> $container<span class="token2">[</span>$mid<span class="token2">]</span> <span class="token"><</span> $search <span class="token">&&</span> $low <span class="token">=</span> $mid <span class="token">+</span> <span class="token3">1</span><span class="token2">;</span> $container<span class="token2">[</span>$mid<span class="token2">]</span> <span class="token">></span> $search <span class="token">&&</span> $top <span class="token">=</span> $mid <span class="token">-</span> <span class="token3">1</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> ``` ``` 递归版 二分查找 ``` <pre class="calibre16">``` <span class="token5">function</span> <span class="token1">BinaryQueryRecursive</span><span class="token2">(</span>array $container<span class="token2">,</span> $search<span class="token2">,</span> $low <span class="token">=</span> <span class="token3">0</span><span class="token2">,</span> $top <span class="token">=</span> <span class="token4">'default'</span><span class="token2">)</span> <span class="token2">{</span> $top <span class="token">==</span> <span class="token4">'default'</span> <span class="token">&&</span> $top <span class="token">=</span> <span class="token1">count</span><span class="token2">(</span>$container<span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span>$low <span class="token"><=</span> $top<span class="token2">)</span> <span class="token2">{</span> $mid <span class="token">=</span> <span class="token1">intval</span><span class="token2">(</span><span class="token1">floor</span><span class="token2">(</span>$low <span class="token">+</span> $top<span class="token2">)</span> <span class="token">/</span> <span class="token3">2</span><span class="token2">)</span><span class="token2">;</span> <span class="token5">if</span> <span class="token2">(</span><span class="token">!</span><span class="token1">isset</span><span class="token2">(</span>$container<span class="token2">[</span>$mid<span class="token2">]</span><span class="token2">)</span><span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token4">'没找着哦'</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span> <span class="token2">(</span>$container<span class="token2">[</span>$mid<span class="token2">]</span> <span class="token">==</span> $search<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> $mid<span class="token2">;</span> <span class="token2">}</span> <span class="token5">if</span> <span class="token2">(</span>$container<span class="token2">[</span>$mid<span class="token2">]</span> <span class="token"><</span> $search<span class="token2">)</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">BinaryQueryRecursive</span><span class="token2">(</span>$container<span class="token2">,</span> $search<span class="token2">,</span> $mid <span class="token">+</span> <span class="token3">1</span><span class="token2">,</span> $top<span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token5">else</span> <span class="token2">{</span> <span class="token5">return</span> <span class="token1">BinaryQueryRecursive</span><span class="token2">(</span>$container<span class="token2">,</span> $search<span class="token2">,</span> $low<span class="token2">,</span> $mid <span class="token">-</span> <span class="token3">1</span><span class="token2">)</span><span class="token2">;</span> <span class="token2">}</span> <span class="token2">}</span> <span class="token2">}</span> ``` ```