<?php
$array=array('2','4','5','1','3');
echo "Unsorted array is: ";
echo "<br />";
print_r($array);
for($j = 0; $j < count($array); $j ++) {
echo "#"; // prints '#' for 5 times
for($i = 0; $i < count($array)-1; $i ++){
echo "@"; // prints '@' 20 times
if($array[$i] > $array[$i+1]) {
$temp = $array[$i+1];
$array[$i+1]=$array[$i];
$array[$i]=$temp;
}
}
}
echo "Sorted Array is: ";
echo "<br />";
print_r($array);
?>
Above is the code for sorting an array in ascending order using bubble sort. How to reduce the no of iterations while sorting an array in ascending order ?
I want to reduce the iterations of '@' symbol in inner loops!
Or can this sorting be done in minimum iterations ?
I do not want to use php in-built functions for sorting an array