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

Re: Quicksort problem

by ansh batra (Friar)
on Nov 22, 2011 at 06:14 UTC ( #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

Comment on Re: Quicksort problem
Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (17)
As of 2015-07-31 13:59 GMT
Find Nodes?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...

    Results (278 votes), past polls