Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re (tilly) 1: Simple bubble sort

by tilly (Archbishop)
on Sep 20, 2001 at 07:43 UTC ( [id://113522]=note: print w/replies, xml ) Need Help??


in reply to Simple bubble sort

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.

Replies are listed 'Best First'.
Re: Re (tilly) 1: Simple bubble sort
by Anonymous Monk on Nov 12, 2014 at 19:19 UTC
    Glad this is at the end. Couldnt take much more.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://113522]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-25 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found