Bubble sorting-php and bubble-php. Bubble sorting-php, bubble-php implementation code: functionbubble_sort ($ array) {for ($ i0; $ icount ($ array)-1; $ I ++) {$ I refers to the number of elements that have been sorted in ascending order-php, bubble-php
PHP implementation code is provided first:
Function bubble_sort ($ array) {for ($ I = 0; $ I <count ($ array)-1; $ I ++) {// $ I is 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. use the total length minus $ I if ($ array [$ j]> $ array [$ j + 1]). {// Sort by 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:
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 first: function bubble_sort ($ array) {for ($ I = 0; $ I count ($ array)-1; $ I ++) {// $ I indicates the number of elements that have already been sorted...