シェーカーソート
これは結構おもしろかった
use strict; use warnings; my $trials = 15; my %h = map { int rand $trials => 1 } 1..$trials; my @target = keys %h; print join(" ", @target); print "\n"; @target = shaker_sort(@target); sub shaker_sort { my @ary = @_; my ($begin, $end) = (0, $#ary); my $flag = 1; while($flag) { undef $flag; my $temp = $end; for my $i ($begin..$end-1) { next if $ary[$i] < $ary[$i+1]; ($ary[$i], $ary[$i+1]) = ($ary[$i+1], $ary[$i]); $temp = $i; $flag = 1; print join(" ", @ary); print "\n"; } $end = $temp; $temp = $begin; for my $i (reverse $begin+1..$end) { next if $ary[$i-1] < $ary[$i]; ($ary[$i-1], $ary[$i]) = ($ary[$i], $ary[$i-1]); $temp = $i; $flag = 1; print join(" ", @ary); print "\n"; } $begin = $temp; } @ary; }
6 11 3 12 14 8 0 13 6 3 11 12 14 8 0 13 6 3 11 12 8 14 0 13 6 3 11 12 8 0 14 13 6 3 11 12 8 0 13 14 6 3 11 12 0 8 13 14 6 3 11 0 12 8 13 14 6 3 0 11 12 8 13 14 6 0 3 11 12 8 13 14 0 6 3 11 12 8 13 14 0 3 6 11 12 8 13 14 0 3 6 11 8 12 13 14 0 3 6 8 11 12 13 14