"Jonathan E. Paton" wrote:
> > > @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. I am not sure what you mean by this, sorting is still O(nlgn) even if it is implemented in machine code. The point I was trying to make is, it is not intuitive to sort a list to find the max and min element. > > > > 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. I remember it being mentioned in this list that perl's sort implementation is a mixture of merge and quick sort (I apologize if this is not true). Can you point me to the file in which sort has been implemented. > > > However, you if you cared for speed you'd use the > Math modules on CPAN for this... or Perl Data > Language (PDL). I guess the module you are mentioning here is List::Util. It also uses a method similar to what you have mentioned below. > > > 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] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]