PHP 계수정렬 구현

Jmnote bot (토론 | 기여)님의 2018년 8월 27일 (월) 01:56 판 (봇: 카운팅정렬을(를) 계수정렬(으)로 분류 대체함)

1 개요

PHP 카운팅정렬 구현
<?php
function counting_sort(&$a) {
    $min = min($a);
    $max = max($a);
    $count = [];
    for($i=$min; $i<=$max; $i++) $count[$i]=0;
    foreach($a as $x) $count[$x]++;
    for($i=$min, $j=0; $i<=$max; $i++) {
        while( $count[$i]-- > 0 ) $a[$j++] = $i;
    }
}
$arr = [9,1,22,4,0,-1,1,22,100,10];
counting_sort( $arr );
echo implode(' ', $arr);
# -1 0 1 1 4 9 10 22 22 100

2 같이 보기

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