php

$a = [
    3,
    4,
    2,
    8,
    1,
    9
];

$count = count($a)-2;

for($count;$count>0;$count--){
    for($i=0;$i<=$count;$i++){
        if($a[$i]>$a[$i+1]){
            //change position
            $j = $a[$i];
            $a[$i] = $a[$i+1];
            $a[$i+1] = $j;
        }
    }
}

  



function sort22($arr){
        $left = [];
        $right = [];
        $mid = array_shift($arr);
        $count = count($arr);
        for($i=0;$i<$count;$i++){
                if($arr[$i]<=$mid){
                        $left[] = $arr[$i];
                }else{

                        $right[] = $arr[$i];
                }
        }
        if(count($left)>1){
                $left = sort22($left);
        }
        if(count($right)>1){
                $right = sort22($right);
        }
        return array_merge($left,[$mid],$right);
}

  

 

posted @ 2018-11-21 22:42  北落师问  阅读(106)  评论(0编辑  收藏  举报