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

php的一些简单算法程序(冒泡、快速等),php算法程序冒泡

WBOY
发布: 2016-06-13 08:53:05
原创
1147 人浏览过

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><2<span>){
            </span><span>return</span> <span>$array</span><span>;
        }
        </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$len</span>;<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>]<<span>$array</span>[<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.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板