Quiver in fear, for here is my unholy answer :)
#!/usr/bin/perl -w use strict; my @stack=qw(d b f a e c 6); # <-- bottom .. top --> # You are not allowed to modify this. sub rotate_up { local($a,$b); $a=$_[0]; $a--; return if $a<1; $b=pop @stack; splice(@stack, -$a, 0, $b); } sub sortIt { print "before stack is ",(join ", ",@stack),"\n"; my $initLen=pop @stack; my $currLen=$initLen; # strategy: find the largest element, push it to bottom. # reduce size by one, repeat while($currLen) { my $max; my $rot=$currLen; my $maxLoc=-1; #print "Len $currLen, stack is ",(join ",", @stack),"\n"; # find largest element, and save its position while($rot>=1) { my $x=pop @stack; if (!defined $max || ($x gt $max)) { $maxLoc=$rot; $max=$x; } push @stack, $x; rotate_up($rot) if($rot>1); $rot--; #print "examined $x, max is ($maxLoc, $max), stack is ",(j +oin ",", @stack),"\n"; } # bring largest elem back up to top while($maxLoc>1) { rotate_up($maxLoc--); #print "bumping up, stack is ",(join ",", @stack),"\n"; } # push it to bottom rotate_up($currLen--); } push @stack, $initLen; print "Finished, ending stack is ",(join ", ",@stack),"\n"; } # initial test case sortIt(); # test reversed @stack=qw(a b c d e f 6); # <-- bottom .. top --> sortIt(); # test sorted sortIt(); # test 1 element stack @stack=qw(f 1); # <-- bottom .. top --> sortIt(); @stack=qw(a a f a a 5); # <-- bottom .. top --> sortIt(); # empty stack @stack=qw(0); # <-- bottom .. top --> sortIt(); # make sure items below stack aren't touched @stack=qw(dontmoveme1 dontmoveme2 a a f a a 5); # <-- bottom .. top - +-> sortIt();
Works for me!
--
Mike
(Edit: Updated with comments, added test cases, made the sort code into a subroutine to make testing simpler, added "before and after" printouts for testing)

In reply to Re: Algorithm Pop Quiz: Sorting by RMGir
in thread Algorithm Pop Quiz: Sorting by clintp

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.