PHP冒泡算法

function direct_sort($data) {

    $count = count($data);

    if($count == 0)

       return;

    elseif($count == 1)

        return $data;
    for($i = 0; $i < $count; $i ++) {
        for ($j = $i + 1; $j < $count; $j ++) {        
            if ($data[$i] > $data[$j]) {
                $temp = $data[$i];
                $data[$i] = $data[$j];
                $data[$j] = $temp;
            }
        }
    }
    return $data;
}
$array = array(8,2,9,1,3);

print_r(bulle_sort($array));


-------------------------------------------

for($i = 0; $i < $count; $i ++) {

    for ($j = $count - 1; $j > $i; $j --){

        if ($data[$j-1] > $data[$j]){

            $temp = $data[$j-1];

            $data[$j-1] = $data[$j];

            $data[$j] = $temp;

            }

        }

}

 

posted @ 2011-09-20 17:19  末世  阅读(97)  评论(0编辑  收藏  举报