php冒泡排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
    $arr = array(1,4,2,9,0,10,12,3,7);
    foreach($arr as $val)
    {
       echo $val."--";
    }
    echo "<br/>";
    function bubble_sort(&$arr)
    {
        $len = count($arr);
        for($i = 1; $i <= $len - 1; $i++)
        {
            for($j = $i; $j >=0; $j--)
            {
                 if($arr[$j] < $arr[$j-1])
                 {
                      $temp      = $arr[$j];
                      $arr[$j]   = $arr[$j-1];
                      $arr[$j-1] = $temp;
                 }
            }
        }
 
    }
      
    bubble_sort($arr);
    foreach($arr as $val)
    {
       echo $val."--";
    }
   
?>

 

posted @   xshang  阅读(182)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示