Jason Roth wrote:
> I was reading the perldoc for the sort function and I had a few
> questions.  http://perldoc.perl.org/sort.html says that large arrays
> will be shuffled to ensure nlgn runtime.  What is the cutoff for
> "large arrays" and is there a reason that it isn't simply using a
> randomized quicksort?

Download the source and find out for yourself:
http://www.cpan.org/src/stable.tar.bz2   :-)


> Also http://perldoc.perl.org/functions/sort.html says that sort
> returns an alias into the original list and modifying an element of a
> list returned by sort actually modifies the element in the original
> list.  Consider the following code.
> 
> @a = (10,1,9,5,3,4);
> @b = sort { $a <=> $b } @a;
> $b[0]++;
> 
> If I understand the docs then $b[0]++ should modify @a as well but
> this does not seem to be the case.  What is going on here?

You are not modifying an element of a list, you are modifying an element of an
array, and after sort is finished with the list.

$ perl -le'
    @x = ( 40, 20, 50, 30, 10 );
    print "@x";
    $_++ for sort @x;
    print "@x";
'
40 20 50 30 10
41 21 51 31 11

$ perl -le'
    @x = ( 40, 20, 50, 30, 10 );
    print "@x";
    map $_++, sort @x;
    print "@x";
'
40 20 50 30 10
41 21 51 31 11




John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to