ホームページ  >  記事  >  php教程  >  クイックソート: PHP クイックソート

クイックソート: PHP クイックソート

WBOY
WBOYオリジナル
2016-06-21 08:50:151059ブラウズ
<ol class="dp-c"><li class="alt"><span><span><?php </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> <= 1) </span><span class="keyword">return</span><span> </span><span class="vars">$array</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><span class="vars">$len</span><span>;</span><span class="vars">$i</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><span class="vars">$flag</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></span></li></ol>
この記事へのリンク http://www.cxybl.com/html/wlbc/Php/20130319/37248.html



声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。