PHP算法-冒泡排序

function bubble_sort($arr)
{
    $count = count($arr);
    if ($count <= 1) {
        return $arr;
    }
    $times = $count - 1;
    for ($i = 0; $i < $times; $i ++) {
        for ($j = 0; $j < $count - $i - 1; $j ++) { // 第一遍排序最后一个已经是最大值 故$count-$i
            if ($arr[$j] > $arr[$j + 1]) {
                $tmp = $arr[$j];
                $arr[$j] = $arr[$j + 1];
                $arr[$j + 1] = $tmp;
            }
        }
    }
}

猜你喜欢

转载自erntoo.iteye.com/blog/2391822
今日推荐