pobocks has asked for the wisdom of the Perl Monks concerning the following question:
Update: Fixed! Thanks ever so much. This teaches me why NOT to program at 4 O'Clock (Or without strict and warnings).
I've been trying to implement quicksort (As a teaching problem, not for production) in both Perl and Common Lisp. I think I know where I'm wrong with the Common Lisp one, but I'm not sure where I'm going wrong with the Perl one. It seems to pass identical (Or at least identically starting) lists to the later recursions (quicksort(@less) and its fellows.
Here's the code as is, printlines and all:
#!/usr/bin/perl sub quicksorty{ my @list = @_; my @copy = @list; my $length = @list; my @less, @equal, @greater, @answer; if ($length <= 1){ return @list; } my $pivot = $list[0]; print "My pivot is $pivot\n"; foreach (@list){ if ($_ < $pivot){ push(@less, (shift @copy )); } if ($_ == $pivot){ push(@equal, (shift @copy)); } if ($_ > $pivot){ push(@greater, (shift @copy)); } } unshift(@answer, quicksorty(@less)); unshift(@answer, @equals); unshift(@answer, quicksorty(@greater)); return @answer; } quicksorty(5,3,1,6,4,9);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Quicksort trubbles.
by apl (Monsignor) on Mar 18, 2008 at 19:38 UTC | |
|
Re: Quicksort trubbles.
by pc88mxer (Vicar) on Mar 18, 2008 at 19:49 UTC | |
|
Re: Quicksort trubbles.
by jwkrahn (Abbot) on Mar 18, 2008 at 22:16 UTC | |
|
Re: Quicksort trubbles.
by ysth (Canon) on Mar 19, 2008 at 06:33 UTC |