PHP 쉘정렬 구현

Jmnote bot (토론 | 기여)님의 2020년 11월 2일 (월) 02:57 판 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

PHP 쉘정렬 구현
<?php
function shell_sort(&$a) {
    $size = count($a);
    $gap = intdiv($size,2);
    while( $gap > 0 ) {
        for($i=$gap; $i<$size; $i++) {
            $temp = $a[$i];
            $j = $i;
            while( $j>=$gap && $a[$j-$gap]>$temp ) {
                $a[$j] = $a[$j-$gap];
                $j -= $gap;
            }
            $a[$j] = $temp;
        }
        $gap = intdiv($gap,2);
    }
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
shell_sort( $arr );
echo implode(',',$arr);
# -1,0,1,1,4,9,10,22,22,100

2 같이 보기[ | ]

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