Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Numeric sorting WITHOUT <=>

by tobyink (Abbot)
on Oct 10, 2012 at 06:22 UTC ( #998144=note: print w/ replies, xml ) Need Help??


in reply to Numeric sorting WITHOUT <=>

use Data::Dumper; WITHOUT_USING_SPACESHIP: { my @array = qw( 7 100 12 1 4 2 88 007 ); @array = sort { $a>$b ? 1 : -1 } @array; print Dumper \@array; } WITHOUT_USING_SORT_AT_ALL: { my @array = qw( 7 100 12 1 4 2 88 007 ); for my $i (0 .. $#array) { for my $j ($i .. $#array) { @array[$i, $j] = @array[$j, $i] if $array[$i] > $array[$j] +; } } print Dumper \@array; }

What sorting algorithm is that? I can never remember their names. It may be bubble sort.

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


Comment on Re: Numeric sorting WITHOUT <=>
Download Code
Re^2: Numeric sorting WITHOUT <=>
by tobyink (Abbot) on Oct 10, 2012 at 09:30 UTC

    And here's a Perl 5.16 implementation of quicksort...

    use v5.16; my @sorted = (sub { return @_ unless @_ > 1; my $pivot = splice(@_, int(@_/2), 1); my (@small, @big); push @{ $_ < $pivot ? \@small : \@big }, $_ for @_; return (__SUB__->(@small), $pivot, __SUB__->(@big)) })->(@unsorted);

    Can be made a little neater using List::MoreUtils...

    use v5.16; use List::MoreUtils 'part'; my @sorted = (sub { return @_ unless @_ > 1; my $pivot = splice(@_, int(@_/2), 1); my ($small, $big) = part { $_ > $pivot } @_; return (__SUB__->(@$small), $pivot, __SUB__->(@$big)) })->(@unsorted);

    In older Perls, without __SUB__ you can't recursively call a truly anonymous sub, so you need to have some kind of way of referring to the sub, such as assigning it to a lexical variable...

    my @sorted = do { my $SUB; $SUB = sub { return @_ unless @_ > 1; my $pivot = splice(@_, int(@_/2), 1); my (@small, @big); push @{ $_ < $pivot ? \@small : \@big }, $_ for @_; return ($SUB->(@small), $pivot, $SUB->(@big)) }}->(@array);
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      sort uses mergesort by default, not quicksort. That could be a more appropriate choice?

      Your last snippet has a memory leak.

        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'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (10)
As of 2014-12-19 09:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (77 votes), past polls