When we first learned programming, we knew various sorting algorithms. Now let’s take a look at how the PHP bubble algorithm is used in practical applications. Friends in need can simply refer to it.
The code is as follows | Copy code |
header('Content-Type: text/html; charset=utf-8'); "; function mp($a){ //Pass value if it is an array //i=1 i<5 i++ for($i=0;$i * Because the array starts from 0, it needs to be -1 * Then, we need to compare the last digit and the second to last digit, so -2 * Of course -1 is also possible, just change $a[j+1] to $a[j] and then change $a[j] to $a[j-1] */ for ($j=count($a)-2;$j>=$i;$j--){ //Judge the largest number of digits in the array and compare it with the second largest one If ($a[$j+1]<$a[$j]){ echo $a[$j+1],'------is less than-----',$a[$j],'----------before arrangement:',fn( $a); //If the largest one is smaller than the second largest one, change the position echo '------------After arrangement:',fn($a),' '; } } } return $a; } echo fn(mp($a));
?> |