Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (19)
As of 2014-10-21 14:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (105 votes), past polls