Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: how to sort string in perl

by roboticus (Chancellor)
on Dec 05, 2012 at 11:41 UTC ( [id://1007276]=note: print w/replies, xml ) Need Help??


in reply to how to sort string in perl

Priti24:

This should provide you a couple hints:

$ cat bozosort.pl #!/usr/bin/perl use strict; use warnings; my @list = (1, 2, 3, 9, 6, 4, 5, 7, 0); while (@list) { my $idx = int rand @list; next if grep { $list[$idx] > $_ } @list; say splice @list, $idx, 1; }

On my machine, this produces:

$ perl bozosort.pl 0 1 2 3 4 5 6 7 9

You'll want to make appropriate changes to improve performance, use files, etc.

Update: Hung a lantern on it.

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Replies are listed 'Best First'.
Re^2: how to sort string in perl
by Anonymous Monk on Dec 05, 2012 at 17:44 UTC

    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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1007276]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-03-29 22:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found