in reply to Simple bubble sort
While *ahem* inefficient, I think that this probably is more DWIM in different contexts.# 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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re (tilly) 1: Simple bubble sort
by Anonymous Monk on Nov 12, 2014 at 19:19 UTC |