首页 > php教程 > php手册 > 正文

快速排序:PHP 快速排序

WBOY
发布: 2016-06-21 08:50:15
原创
1064 人浏览过
<ol class="dp-c">
<li class="alt"><span><span><?php  </span></span></span></li>
<li><span><span class="comment">//Quick Sort</span><span> </span></span></li>
<li class="alt"><span><span class="keyword">function</span><span> quickSort(</span><span class="keyword">array</span><span> </span><span class="vars">$array</span><span>){ </span></span></li>
<li><span><span class="keyword">static</span><span> </span><span class="vars">$run</span><span> = 0; </span></span></li>
<li class="alt"><span><span class="vars">$len</span><span> = </span><span class="func">count</span><span>(</span><span class="vars">$array</span><span>); </span></span></li>
<li><span><span class="keyword">if</span><span>(</span><span class="vars">$len</span><span> <span class="keyword">return</span><span> </span><span class="vars">$array</span><span>; </span></span></span></li>
<li class="alt"><span><span class="vars">$arrleft</span><span>=</span><span class="keyword">array</span><span>(); </span></span></li>
<li><span><span class="vars">$arrright</span><span>=</span><span class="keyword">array</span><span>(); </span></span></li>
<li class="alt"><span><span class="vars">$flag</span><span> = </span><span class="vars">$array</span><span>[0]; </span></span></li>
<li><span><span class="keyword">for</span><span>(</span><span class="vars">$i</span><span>=1;</span><span class="vars">$i</span><span><span class="vars">$len</span><span>;</span><span class="vars">$i</span><span>++){ </span></span></span></li>
<li class="alt"><span><span class="keyword">if</span><span>(</span><span class="vars">$array</span><span>[</span><span class="vars">$i</span><span>]<span class="vars">$flag</span><span>){ </span></span></span></li>
<li><span><span class="vars">$arrleft</span><span>[]=</span><span class="vars">$array</span><span>[</span><span class="vars">$i</span><span>]; </span></span></li>
<li class="alt"><span>}<span class="keyword">else</span><span>{ </span></span></li>
<li><span><span class="vars">$arrright</span><span>[] = </span><span class="vars">$array</span><span>[</span><span class="vars">$i</span><span>]; </span></span></li>
<li class="alt"><span>} </span></li>
<li><span>} </span></li>
<li class="alt"><span><span class="vars">$arrleft</span><span> = quickSort(</span><span class="vars">$arrleft</span><span>); </span></span></li>
<li><span><span class="vars">$arrright</span><span> = quickSort(</span><span class="vars">$arrright</span><span>); </span></span></li>
<li class="alt"><span><span class="vars">$run</span><span>++; </span></span></li>
<li><span><span class="keyword">return</span><span> </span><span class="func">array_merge</span><span>(</span><span class="vars">$arrleft</span><span>,</span><span class="keyword">array</span><span>(</span><span class="vars">$flag</span><span>),</span><span class="vars">$arrright</span><span>); </span></span></li>
<li class="alt"><span>} </span></li>
<li><span><span class="vars">$data</span><span>= </span><span class="keyword">array</span><span>(10,12,43,32,123,12); </span></span></li>
<li class="alt"><span><span class="vars">$newarr</span><span> = quickSort(</span><span class="vars">$data</span><span>); </span></span></li>
<li><span>print_r(<span class="vars">$newarr</span><span>); </span></span></li>
<li class="alt"><span>?>  </span></li>
</ol>
登录后复制
本文链接http://www.cxybl.com/html/wlbc/Php/20130319/37248.html



相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板