Let's talk about php bubble sorting and php bubble sorting. Let's talk about php bubble sorting and talk about the code implemented by php bubble PHP: Copy the code as follows: functionbubble_sort ($ array) {for ($ i0; $ icount ($ array)-1; $ I ++) {$ I refers to php bubble sorting and php bubble sorting.
PHP implementation code is provided first:
The code is as follows:
Function bubble_sort ($ array ){
For ($ I = 0; $ I <count ($ array)-1; $ I ++) {// $ I indicates the number of elements that have been sorted
For ($ j = 0; $ j <count ($ array)-1-$ I; $ j ++) {// $ j indicates the number of elements to be sorted, subtract $ I from the total length.
If ($ array [$ j]> $ array [$ j + 1]) {// sort 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:
The code is as follows:
I = 0;
J = 0;
If ($ arr [0]> $ arr [1]) => 5> 1. the condition is true and the position is switched to form a new array => 1 5 4 7 j ++
If ($ arr [1]> $ arr [2]) => 5> 4. the condition is true and the position is switched to form a new array => 1 4 5 7 j ++
If ($ arr [2]> $ arr [3]) => 5> 7 conditions are not true, and the array remains unchanged, 1 4 5 7 j ++ j = 3 exit the inner loop, I ++
And so on.
The code implemented by using PHP is provided as follows: function bubble_sort ($ array) {for ($ I = 0; $ I count ($ array)-1; $ I ++) {// $ I is already...