Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: Quicksort problem

by ansh batra (Friar)
on Nov 22, 2011 at 06:14 UTC ( [id://939373]=note: print w/replies, xml ) Need Help??


in reply to Quicksort problem

@end = quicksort(3, 2, 6, 5, 4); foreach (@end) { print "$_ "; } print "\n"; sub quicksort { @arr=@_; if($#arr < 1) { return @arr; } my $pivot=pop(@arr); my @less; my @great; foreach (@arr) { # print "@_"; if ($_<$pivot) { push @less, $_; } else { push @great, $_; } } return quicksort(@less), $pivot, quicksort(@great); }
$#arr returns the index of the last element of @arr.
so if @arr contains 1 element then its index will be 0 i.e < 1

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (8)
As of 2024-04-25 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found