Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^5: Numeric sorting WITHOUT <=>

by ikegami (Pope)
on Oct 10, 2012 at 22:24 UTC ( #998329=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Numeric sorting WITHOUT <=>
in thread Numeric sorting WITHOUT <=>

Actually, a naïve merge sort is dead simple. Quite possibly the easiest to explain, too.

sub mergesort { return $_[0] if @_ == 1; my $i = int( @_ / 2 ); my @a = mergesort(@_[0..$i-1]); my @b = mergesort(@_[$i..$#_]); my @sorted; while (@a && @b) { if ($a[0] < $b[0]) { push @sorted, shift(@a); } elsif ($b[0] < $a[0]) { push @sorted, shift(@b); } else { push @sorted, shift(@a), shift(@b); } } return ( @sorted, @a, @b ); }

lol! I got two 5.8.8 question this week. It'll be a while before it's ok to be sick of 5.14.


Comment on Re^5: Numeric sorting WITHOUT <=>
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (15)
As of 2015-07-31 19:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (280 votes), past polls