Blogger Information
Blog 2
fans 4
comment 0
visits 1489
Related recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
冒泡排序
亚连的博客
Original
766 people have browsed it
基本思想:对需要排序的数组从后往前(逆序)进行多遍的扫描,当发现相邻的两个数值的次序与排序要求的规则不一致时,就将这两个数值进行交换。这样比较小(大)的数值就将逐渐从后面向前面移动。
<?php
function mysort($arr)
{
    for($i = 0; $i < count($arr); $i++)
    {
        $isSort = false;
        for ($j=0; $j< count($arr) - $i - 1; $j++) //经历过2次循环后,在对相邻的2个值进行判断,如果前面的小于后面的位置互换 
        {
            if($arr[$j] < $arr[$j+1])//判断数组相邻的2个数值大小
            
            {
                $isSort = true;
                $temp = $arr[$j];
                $arr[$j] = $arr[$j+1];
                $arr[$j+1] = $temp ;
            }
        }
       if($isSort)
       {
         break;
       }
    }
    return $arr;
}
$arr = array(3,1,2);
var_dump(mysort($arr));
?>


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!