Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Numeric sorting WITHOUT <=>

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


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

sort uses mergesort by default, not quicksort. That could be a more appropriate choice?

Your last snippet has a memory leak.

Replies are listed 'Best First'.
Re^4: Numeric sorting WITHOUT <=>
by tobyink (Abbot) on Oct 10, 2012 at 21:43 UTC

    Mergesort is more complex to implement I think, though not massively so. Perl did once use quicksort, but it was changed in 5.8. The old sorting algorithm can be enabled using:

    use sort '_quicksort';

    Memory leak... indeed. Is it too early to be getting sick of pre-5.16 Perls? In practice you'd probably give the sub a name so this wouldn't be an issue.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://998282]
help
Chatterbox?
[LanX]: Choroba: do you miss chaos with ties? apply at the US government.. ;)
[ambrus]: Corion: those are good rules.
[ambrus]: Discipulus: oh sure. the input data has different filenames every time I get them.
[ambrus]: the directory structure may be 1, 2, or 3 deep, it may have spaces in the filename or not, it has dates in various format, different keywords for the same meanings, and the dates and other keywords are assembled in various ways.
[Discipulus]: no ambrus by specification i mean for example license per core instead of per socket, so fields are different

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2017-03-29 12:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (350 votes). Check out past polls.