数据结构-插入排序 可刷新运行 刷新

代码:

$a= [34,8,64,51,32,21];

print_r(insertionSort($a,count($a)));

function insertionSort($a,$n){
    for ($i=1;$i<$n;$i++){
        $tmp=$a[$i];
        for($j=$i;$j>0&&$a[$j-1]>$tmp;$j--){
            $a[$j]=$a[$j-1];
        }
        $a[$j]=$tmp;
    }
    return $a;
}

输出:

Array
(
    [0] => 8
    [1] => 21
    [2] => 32
    [3] => 34
    [4] => 51
    [5] => 64
)
遇到问题?