数据结构-希尔增量的希尔排序 可刷新运行 刷新

代码:

$a= [81,94,11,96,12,35,17,95,28,58,41,75,15];

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

function shellSort($a,$n){
    for ($increment=(int)($n/2);$increment>0;($increment=(int)($increment/2))){
        for ($i=$increment;$i<$n;$i++){
            $tmp=$a[$i];
            for($j=$i;$j>=$increment;$j-=$increment){
                if($tmp<$a[$j-$increment]){
                    $a[$j]=$a[$j-$increment];
                }else{
                    break;
                }
            }
            $a[$j]=$tmp;
        }
    }

    return $a;
}

输出:

Array
(
    [0] => 11
    [1] => 12
    [2] => 15
    [3] => 17
    [4] => 28
    [5] => 35
    [6] => 41
    [7] => 58
    [8] => 75
    [9] => 81
    [10] => 94
    [11] => 95
    [12] => 96
)