You can make it simpler with loop control. And I threw in
some behaviour tricks, see if you can figure it out. :-)
# Simple bubble sort, written with an empty loop and with
# some convenience tricks depending on how it is called
sub bbl_sort {
if (defined wantarray) {
@_ = wantarray ? @_ : @{shift(@_)};
}
SCAN: {
foreach (0..(@_-2)) {
if ($_[$_] gt $_[$_+1]) {
@_[$_, $_+1] = @_[$_+1, $_];
redo SCAN;
}
}
}
wantarray ? @_ : [@_];
}
# Try it, showing one of the games
my ($first, $second, $third) = qw(not in order?);
bbl_sort($first, $second, $third);
print map "$_\n", $first, $second, $third;
While
*ahem* inefficient, I think that this probably
is more DWIM in different contexts.