Rumah >pembangunan bahagian belakang >tutorial php >php的一些简单算法程序(冒泡、快速等)
<span>function</span> buttle_sort(<span>$array</span><span>) { </span><span>$len</span>=<span>count</span>(<span>$array</span><span>); </span><span>if</span>(<span>$len</span>){ <span>return</span><span>$array</span><span>; } </span><span>for</span>(<span>$i</span>=0;<span>$i</span>$len;<span>$i</span>++<span>){ </span><span>$flag</span> = <span>false</span>;<span>//</span><span>本趟排序开始前,交换标志应为假</span><span>for</span>(<span>$j</span>=<span>$len</span>-1;<span>$j</span>><span>$i</span>;<span>$j</span>--<span>){ </span><span>if</span>(<span>$array</span>[<span>$j</span>]$array[<span>$j</span>-1<span>]){ </span><span>$tmp</span> = <span>$array</span>[<span>$j</span><span>]; </span><span>$array</span>[<span>$j</span>] = <span>$array</span>[<span>$j</span>-1<span>]; </span><span>$array</span>[<span>$j</span>-1] = <span>$tmp</span><span>; </span><span>$flag</span> = <span>true</span>;<span>//</span><span>发生了交换,故将交换标志置为真</span><span> } } } </span><span>if</span>(!<span>$flag</span>)<span>//</span><span>本趟排序未发生交换,提前终止算法</span><span>return</span><span>$array</span><span>; }</span>
待补充
以上就介绍了php的一些简单算法程序(冒泡、快速等),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。