in reply to max of N numbers?
This takes linear time. Or you can use a sort, which takes O(n lg n) time:use strict; use warnings; my @arr = (2,7,12,5,17,9); print max(@arr); sub max { my $max = $_[0]; do { $max = $_[$_] if $max < $_[$_]; } for 1..$#_; $max; }
my $max = (sort { $b <=> $a } @arr)[0];
|
|---|