Perl 선택정렬 구현

1 개요[ | ]

Perl 선택정렬 구현
sub selection_sort {
    my $a=shift;
    $size=@$a;
    for $i (0..$size-1) {
        $minidx=$i;
        for $j ($i+1..$size-1) {
            $minidx=$j if (@$a[$minidx]>@$a[$j]);
        }
        (@$a[$minidx],@$a[$i]) = (@$a[$i],@$a[$minidx]);
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
selection_sort(\@arr);
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100
@arr = (9,1,22,4,0,-1,1,22,100,10);
$size=@arr;
for $i (0..$size-1) {
    $minidx=$i;
    for $j ($i+1..$size-1) {
        $minidx=$j if ($arr[$minidx]>$arr[$j]);
    }
    ($arr[$minidx],$arr[$i]) = ($arr[$i],$arr[$minidx]);
}
print join(',', @arr);
# -1,0,1,1,4,9,10,22,22,100

2 같이 보기[ | ]

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