Find Laregest And 2nd Largest element in an array without function in Php
<?php
$testarray = array(4,3,8,5,2,9,7,6);
$length = count($testarray);
for($i=0;$i<$length;$i++){
for($j=0;$j<$length-$i-1;$j++){
if ($testarray[$j]>$testarray[$j+1]) {
$temp = $testarray[$j];
$testarray[$j] = $testarray[$j+1];
$testarray[$j+1] = $temp;
}
}
}
echo "Largest Element == ".$testarray[$length-1+1-1];
echo"<br>";
echo "2nd Largest Element == ".$testarray[$length-2+1-1];
?>
OutPut:-
2nd Largest Element == 8
Comments
Post a Comment