JediMasterT has asked for the wisdom of the Perl Monks concerning the following question:
I've just gotten back to perl after about a year, and I'm a little rusty, so bear with me
For some fun practice, I have trying to implement Quicksort. Here is the code:
@end = quicksort(3, 2, 6, 5, 4); foreach (@end) { print "$_ "; } print "\n"; sub quicksort { my $pivot=pop; my @less; my @great; foreach (@_) { if ($_<$pivot) { push @less, $_; } else { push @great, $_; } } return quicksort(@less), $pivot, quicksort(@great); }
However, when I run the sub (with any array), it loops infinity, until perl runs out of memory and shuts down. Help?
UPDATE: for all (if any) looking for an answer, I didn't have a return @_ if (@#_<1); in the first line of my sub, so I never had a terminating statement. Thank you for your time.
JAPH,
JediMasterT
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Quicksort problem
by BrowserUk (Patriarch) on Nov 22, 2011 at 05:49 UTC | |
by JediMasterT (Sexton) on Nov 22, 2011 at 13:49 UTC | |
by Limbic~Region (Chancellor) on Nov 22, 2011 at 14:12 UTC | |
by choroba (Cardinal) on Nov 22, 2011 at 14:13 UTC | |
|
Re: Quicksort problem
by ansh batra (Friar) on Nov 22, 2011 at 06:14 UTC | |
|
Re: Quicksort problem
by hbm (Hermit) on Nov 22, 2011 at 14:53 UTC | |
by JediMasterT (Sexton) on Nov 22, 2011 at 20:56 UTC | |
|
Re: Quicksort problem
by JediMasterT (Sexton) on Nov 22, 2011 at 13:41 UTC | |
|
Re: Quicksort problem
by Khen1950fx (Canon) on Nov 22, 2011 at 09:44 UTC | |
|
Re: Quicksort problem
by Marshall (Canon) on Nov 22, 2011 at 08:50 UTC | |
by MidLifeXis (Monsignor) on Nov 22, 2011 at 11:30 UTC | |
by JediMasterT (Sexton) on Nov 23, 2011 at 02:32 UTC |