Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

how to sort string in perl

by Priti24 (Novice)
on Dec 05, 2012 at 11:22 UTC ( [id://1007268]=perlquestion: print w/replies, xml ) Need Help??

Priti24 has asked for the wisdom of the Perl Monks concerning the following question:

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: how to sort string in perl
by marto (Cardinal) on Dec 05, 2012 at 11:26 UTC
Re: how to sort string in perl
by rovf (Priest) on Dec 05, 2012 at 11:30 UTC
    In addition to what marto wisely said:

    Just writing plz help me is a bit unspecific. You need to explain more in detail, at which point you got stuck.

    -- 
    Ronald Fischer <ynnor@mm.st>
    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: how to sort string in perl
by roboticus (Chancellor) on Dec 05, 2012 at 11:41 UTC

    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.

      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";
Re: how to sort string in perl
by Arunbear (Prior) on Dec 05, 2012 at 11:58 UTC
Re: how to sort string in perl
by karlgoethebier (Abbot) on Dec 05, 2012 at 13:26 UTC
Re: how to sort string in perl
by davido (Cardinal) on Dec 05, 2012 at 16:48 UTC

    You're in luck. If someone were asking me to sort strings, sort would be my second choice too. My first choice would probably be Unicode::Collate. Wrap your solution around that module, and you've satisfied the requirement of not using sort, while at the same time providing a better solution that sort would have offered in the first place.

    I'm aware that this answer probably is of little help to you in your interview; the interviewer probably wants to see you write a sort. But if he's interested in getting the job done well, maybe he will respect the answer. ;)


    Dave

Re: how to sort string in perl
by Anonymous Monk on Dec 05, 2012 at 13:31 UTC
    $ sort <infile >outfile
Re: how to sort string in perl
by Plankton (Vicar) on Dec 28, 2012 at 00:13 UTC
    not sorting them is sort of a sort

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1007268]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 06:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found