"High watermark" algorithm: After the flood, the last wave subsided, the high waterline will indicate the highest water level seen.
Here's a look at the "high watermark" algorithm used in Perl.
#! /usr/bin/perl;
Use UTF8;
Sub Max {my
($max _so_far) = Shift @_; #数组中第一个值, temporarily as the maximum value.
foreach (@_) { #遍历数组 @_
if ($_> $max _so_far) { #看其它元素是否有比 $max _so_far large value.
$max _so_far = $_;} #如果有话, update the maximum value variable
}
$max _so_far;
}
My $_maxdata = &max (2,3,8,5,10);
The first row of the array @_ is shifted, placing an element 2 in the maximum $max_so_far variable, the remaining element in the @_ is (3,8,5,10), then looping through the arrays with a foreach loop, the first element in the new array is 3:2 large, and is moved to the $max_so_ In the far variable, and so on, the last 10 is the largest element in the array.