This is certainly one of those programming I did that for fun. I was just trying to get the shortest insertion sort in perl. Obviously it's still too long compared to what I can find in stackoverflow. (BTW, it's a great place to learn from good programmers.)
Insertion Sort 72 (Perl): Bad
@A=(-10,0,50,7,8,8,1,2,3);
for($j=1;$j<scalar(@A);$j++){
($k,$i)=($A[$j],$j-1);
($A[$i+1],$i)=($A[$i],$i-1) while($i>=0&&$A[$i]>$k);
$A[$i+1]=$k;
}
printf("%s\n",join(" ",@A));
for($j=1;$j<scalar(@A);$j++){
($k,$i)=($A[$j],$j-1);
($A[$i+1],$i)=($A[$i],$i-1) while($i>=0&&$A[$i]>$k);
$A[$i+1]=$k;
}
printf("%s\n",join(" ",@A));
Here is a 72 chars from stackoverflow
sub i{my$m;$_[$_]<$_[$m]&&($m=$_)for 1..$#_;(splice(@_,$m,1),@_?i@_:@_)}
No comments:
Post a Comment