> > @data = (10,45,2,439);
> > ($min,$max) = (sort {$a <=> $b} @data)[0,$#data];
> 
> IMHO sorting a list to find the max and min
> element is not a good idea.  The sort is of
> O(nlgn) and the usual run through the list
> to find the max element is O(n).

In general, it's not.

> This is not of significance when n is small,
> at the same time this is not a method that
> should be followed for this task.

Sort isn't implemented in perl's bytecode, but in
machine code.  Machine code runs a few times faster
that your scripts can, hence for small datasets the
sort is faster.

However, you if you cared for speed you'd use the
Math modules on CPAN for this... or Perl Data
Language (PDL).

If you don't, and it's production code, then use:

sub maxmin {
    my @array = @_;
    my ($max, $min) = @array[0, 0];

    foreach my $element (@array) {
        $max = $element
            if $element > $max;
        $min = $element
            if $element < $min;
    }
    return ($max, $min);
}




__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to