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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.