The principle of bubble sort is
to compare two adjacent elements each time and swap the larger element to the right end.
Bubble sort execution process output effect
Bubble sort implementation ideas
Each bubble sorting operation will compare two adjacent elements to see if they meet the size relationship requirements. If not, the order of the two adjacent elements will be exchanged. At least one element will be moved to where it should be in a bubble sort operation. Repeat the arranged position N times to complete the bubble sort.
bubble sort code
$array=[2,31,4,6,1,8,21,34,23];
for($i=0; $i<count($array)-1; $i++){
for($ij=0; $ij<count($array)-1-$i; $ij++){
if ($array[$ij] > $array[$ij+1]){
$temp = $array[$ij];
$array[$ij]=$array[$ij+1];
$array[$ij+1]=$temp;
}
}
}