F
Feyruz
hello,
can someone show me other ways to find the max. element in an array
recursively? Other than shown in the code below? (for educational
purposes). I can imagine that it is not the best way because of memory
usage.
use strict;
use warnings;
#Author: Feyruz Yalcin/Groningen/Netherlands
#Algorithmica Exerc. R-4.7. P.179
sub max {
my @ar = @_ if ( @_ );
if ( $#ar==0 ) {
return $ar[0];
} else {
my $max = pop( @ar );
my $other = max( @ar );
if ( $max > $other ) {
return $max;
} else {
return $other;
}
}
}
#example run
my @price = (10, 10, 6, 3, 4, 2, 5, 7, 132, 3, 10, 6, 3, 4, 2, 10, 6,
3, 4, 2, 5, 7, 5, 7, 4, 2, 5,6, 3, 4, 2, 10, 6, 3, 4, 2, 5, 7, 5, 7, 4,
2, 5,7);
print max(@price);
can someone show me other ways to find the max. element in an array
recursively? Other than shown in the code below? (for educational
purposes). I can imagine that it is not the best way because of memory
usage.
use strict;
use warnings;
#Author: Feyruz Yalcin/Groningen/Netherlands
#Algorithmica Exerc. R-4.7. P.179
sub max {
my @ar = @_ if ( @_ );
if ( $#ar==0 ) {
return $ar[0];
} else {
my $max = pop( @ar );
my $other = max( @ar );
if ( $max > $other ) {
return $max;
} else {
return $other;
}
}
}
#example run
my @price = (10, 10, 6, 3, 4, 2, 5, 7, 132, 3, 10, 6, 3, 4, 2, 10, 6,
3, 4, 2, 5, 7, 5, 7, 4, 2, 5,6, 3, 4, 2, 10, 6, 3, 4, 2, 5, 7, 5, 7, 4,
2, 5,7);
print max(@price);