简单分析冒泡排序



$arr = [3,6,4,2,11,10,5];

function mp_sort($arr){
    $len = count($arr);
    for ($i=0; $i < $len; $i++) { 
    	//此处加入一个优化代码,当数组不需要再交换值时就表示排序已经完成,可以在下方提前退出排序循环
        $flag = false;
        //冒泡排序每次都会将‘最大值’放在末尾 所以循环的时候 后面的位置都是排序好的 
        //$len-$i-1 就是将排序好的值忽略 
        //这么做是为了优化算法 不加也可但是空间时间复杂度都会提高
        for ($j=0; $j < $len-$i-1; $j++) { 
            echo $j."-".($j+1)."\r\n";
            print_r($arr);
            if($arr[$j] > $arr[$j+1]){
                // echo $j."-".($j+1)."\r\n";
                $temp = $arr[$j+1];
                $arr[$j+1] = $arr[$j];
                $arr[$j] = $temp;
                $flag = true;
            }
        }
        if(!$flag){
            break;
        }
        echo str_repeat("---",20)."\r\n";
    }
    return $arr;
}


// echo str_repeat("****",20)."\r\n";
mp_sort($arr);



图中的每一趟都是由外层的$i进行控制的
每次比较都是$j来控制的
简单分析冒泡排序_第1张图片

运行时每一次的数组变化

0-1
Array
(
    [0] => 3
    [1] => 6
    [2] => 4
    [3] => 2
    [4] => 11
    [5] => 10
    [6] => 5
)
1-2
Array
(
    [0] => 3
    [1] => 6
    [2] => 4
    [3] => 2
    [4] => 11
    [5] => 10
    [6] => 5
)
2-3
Array
(
    [0] => 3
    [1] => 4
    [2] => 6
    [3] => 2
    [4] => 11
    [5] => 10
    [6] => 5
)
3-4
Array
(
    [0] => 3
    [1] => 4
    [2] => 2
    [3] => 6
    [4] => 11
    [5] => 10
    [6] => 5
)
4-5
Array
(
    [0] => 3
    [1] => 4
    [2] => 2
    [3] => 6
    [4] => 11
    [5] => 10
    [6] => 5
)
5-6
Array
(
    [0] => 3
    [1] => 4
    [2] => 2
    [3] => 6
    [4] => 10
    [5] => 11
    [6] => 5
)
------------------------------------------------------------
0-1
Array
(
    [0] => 3
    [1] => 4
    [2] => 2
    [3] => 6
    [4] => 10
    [5] => 5
    [6] => 11
)
1-2
Array
(
    [0] => 3
    [1] => 4
    [2] => 2
    [3] => 6
    [4] => 10
    [5] => 5
    [6] => 11
)
2-3
Array
(
    [0] => 3
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 10
    [5] => 5
    [6] => 11
)
3-4
Array
(
    [0] => 3
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 10
    [5] => 5
    [6] => 11
)
4-5
Array
(
    [0] => 3
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 10
    [5] => 5
    [6] => 11
)
------------------------------------------------------------
0-1
Array
(
    [0] => 3
    [1] => 2
    [2] => 4
    [3] => 6
    [4] => 5
    [5] => 10
    [6] => 11
)
1-2
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 6
    [4] => 5
    [5] => 10
    [6] => 11
)
2-3
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 6
    [4] => 5
    [5] => 10
    [6] => 11
)
3-4
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 6
    [4] => 5
    [5] => 10
    [6] => 11
)
------------------------------------------------------------
0-1
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 10
    [6] => 11
)
1-2
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 10
    [6] => 11
)
2-3
Array
(
    [0] => 2
    [1] => 3
    [2] => 4
    [3] => 5
    [4] => 6
    [5] => 10
    [6] => 11
)

你可能感兴趣的:(php,通用技巧,冒泡排序)