Code to find nth largest number in an array
- by user136104
I have written following code in perl
#!/usr/bin/perl
@array = (3,6,8,1,2);
my $second_largest = 0;
my $largest = 0;
for (@array)
{
if($_ > $largest)
{
$second_largest = $largest;
$largest = $_;
}
if($_ > $second_largest && $_ < $largest)
{
$second_largest = $_;
}
}
print "Second largest::".$second_largest;
print "largest::".$largest;
But I need a general code to find out "Nth" largest and smallest number of an array
Please help me