The code implemented by PHP is first served:
Copy Code code as follows:
function Bubble_sort ($array) {
for ($i = 0; $i < count ($array)-1; $i + +) {//$i the number of elements that have been ordered
for ($j = 0; $j < count ($array)-1-$i; $j + +) {//$j the number of elements that need to be sorted, with the total length minus $i
if ($array [$j] > $array [$j + 1]) {//in ascending order
$temp = $array [$j];
$array [$j] = $array [$j + 1];
$array [$j + 1] = $temp;
}
}
}
return $array;
}
$a = Array (5, 1, 4, 7);
Code Execution Process:
Copy Code code as follows:
i = 0;
j = 0;
if ($arr [0] > $arr [1]) => 5 > 1 condition set up, swap position, form new array => 1 5 4 7 J + +
if ($arr [1] > $arr [2]) => 5 > 4 condition set up, swap position, form new array => 1 4 5 7 J + +
if ($arr [2] > $arr [3]) => 5 > 7 conditions are not set, the array remains unchanged, 1 4 5 7 j + + J=3 exits the inner loop, i++
Let's turn it over.