Some simple algorithm programs in php (bubble, fast, etc.), php algorithm program bubbling_PHP tutorial

WBOY
Release: 2016-07-12 09:07:08
Original
823 people have browsed it

Some simple algorithm programs in php (bubble, fast, etc.), php algorithm program bubbling

<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>
Copy after login

To be added

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1062611.htmlTechArticleSome simple algorithm programs in php (bubble, fast, etc.), php algorithm program bubble function buttle_sort( $array ) { $len = count ( $array ); if ( $len 2 ){ return $array ; } for ( $i =...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!