If you just want to use the quicksort algorithm on a standard perl array, then prior to (I think) 5.8.0, perl's built-in sort used that algorithm. From 5.8.0 on, you have to request the quicksort algorithm be used in preference to the now default mergesort using the sort pragma

use sort qw[ _quicksort ];

In the case of the post to which you refer, the OP was looking to save space by storing integers in a packed (or veced) scalar. As the built-in sort doesn't know how to deal with these, it becomes necessary to implement the qsort algorithm yourself. This is the code I knocked up back then to test the performance of a pure perl implementation using vec.

#! perl -slw use strict; use Benchmark qw[ timethis ]; local $, = ' '; our $SIZE ||= 4; our $N ||= 100; $N--; sub qSortVec { my( $rVec, $size, $left, $right ) = @_; if( $left < $right ) { my( $u, $d ) = ( $left, $right ); my $pivot = vec( $$rVec, ( $u + $d ) / 2, $size ); do { $u++ while ( vec( $$rVec, $u, $size ) < $pivot ); $d-- while ( vec( $$rVec, $d, $size ) > $pivot ); if( $u <= $d ) { my $temp = vec( $$rVec, $u, $size ); vec( $$rVec, $u, $size ) = vec( $$rVec, $d, $size ); vec( $$rVec, $d, $size ) = $temp; $u++; $d--; } } while $u <= $d; qSortVec ( $rVec, $size, $left, $d ); qSortVec ( $rVec, $size, $u, $right ); } } srand( 1 ); our $vec =''; vec( $vec, $_, $SIZE ) = int rand( 1 << $SIZE ) for 0 .. $N; print map{ vec( $vec, $_, $SIZE ) } 0 .. $N; our $copy; timethis( 10, q[ $copy = $vec; qSortVec( \$copy , $SIZE, 0, $N ) ] ); print split"(.{$SIZE})", unpack 'B*', $vec; print map{ vec( $copy, $_, $SIZE ) } 0 .. $N; my $res = 0; $res |= vec( $copy, $_, $SIZE ) > vec( $copy, $_+1, $SIZE ) for 0 .. +$N-1; print 'Sort error' if $res;


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail

In reply to Re: Re: Re: Re: Re: Re: Re: sorting a vec by BrowserUk
in thread sorting a vec by mcinnes

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.