Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: how to sort string in perl

by Anonymous Monk
on Dec 05, 2012 at 17:44 UTC ( #1007345=note: print w/replies, xml ) Need Help??


in reply to Re: how to sort string in perl
in thread how to sort string in perl

I like stooge sort more. It can be much more efficient: I used functional-programming techniques which means it can be trivially parallelised over many CPUs.

sub stoogesort { my (@lst) = @_; if ($lst[-1] < $lst[0]) { # swap @lst[0, -1] = @lst[-1, 0]; } return @lst if (@lst < 3); # thirds my $zero = 0; my $one = int(@lst / 3); my $two = int($#lst * 2 / 3); my $three = $#lst; for my $range ( [ $zero .. $two ], [ $one .. $three ], [ $zero .. $two ] ) { @lst[ @$range ] = stoogesort( @lst[ @$range ] ); } return @lst; } my @list = (1, 2, 3, 9, 6, 4, 5, 7, 0); print join(" ", stoogesort(@list)), "\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007345]
help
Chatterbox?
[Lady_Aleena]: Module is currently 104 lines but will shirnk to 63. The script using the module is currently 40 lines but will grow to 82, 180 to 146 lines total. (This is after rewrting the data files.)

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2017-05-29 03:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?