Perl 쉘정렬 구현

1 개요[ | ]

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

2 같이 보기[ | ]

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