Copy code The code is as follows:
$arr = array(345,4,17,6,52,16,58,69,32,8,234);
$n = count( $arr);
for($i=1;$i<$n;$i++){
//Why $n-1 is because the array is calculated from 0
//Next is the first time Inner loop
for($j=$n-1;$j>=$i;$j--)
{
//If $arr[10]<$arr[9];
//temp = $ arr[9];
if($arr[$j]<$arr[$j-1]){
//$temp Put the small values together for now
$temp = $arr[$j-1 ];
//This is the time to exchange positions
$arr[$j-1] = $arr[$j];
//$arr[9] = the value of $arr[10]
$arr[$ j] = $temp;
//The value of $arry[10] is equal to the value of $arr[9]
//At this time, the positions will begin
}
}
}
?>
The above has introduced the bubble sort algorithm and a detailed interpretation of the PHP bubble sort algorithm code, including the content of the bubble sort algorithm. I hope it will be helpful to friends who are interested in PHP tutorials.