PHP 퀵정렬 구현

1 개요[ | ]

PHP 퀵정렬 구현
function quick_sort(&$a, $L=0, $R=-1) {
    if($R==-1) $R=count($a)-1;
    $pivot = $a[$L];
    for($left=$L,$right=$R; $left<$right; $right--) {
        while( $a[$right]>=$pivot && $left<$right ) $right--;
        if( $left<$right ) $a[$left] = $a[$right];
        while( $a[$left]<=$pivot && $left<$right ) $left++;
        if( $left>=$right ) break;
        $a[$right] = $a[$left];
    }
    $a[$left]=$pivot;
    if($left>$L) quick_sort($a, $L, $left-1);
    if($left<$R) quick_sort($a, $left+1, $R);
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
quick_sort( $arr );
echo implode(',',$arr); # -1,0,1,1,4,9,10,22,22,100

2 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}