Perl 삽입정렬 구현

1 개요[편집]

Perl 삽입정렬 구현
펄 삽입정렬 구현
sub insertion_sort {
    my $a=shift;
    $size=@$a;
    for $i (1..$size-1) {
        $j=$i-1;
        $temp=@$a[$i];
        while ($j>=0 && @$a[$j]>$temp) {
            @$a[$j+1]=@$a[$j];
            $j-=1;
        }
        @$a[$j+1]=$temp;
    }
}
@arr = (9,1,22,4,0,-1,1,22,100,10);
insertion_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 (1..$size-1) {
    $j=$i-1;
    $temp=$arr[$i];
    while ($j>=0 && $arr[$j]>$temp) {
        $arr[$j+1]=$arr[$j];
        $j-=1;
    }
    $arr[$j+1]=$temp;
}
print join(',',@arr);
# -1,0,1,1,4,9,10,22,22,100

2 같이 보기[편집]

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