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);
In reply to Quicksort trubbles. by pobocks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |