Bubble sort-php, bubbling-php
the code implemented by PHP is first:
functionBubble_sort ($array) { for($i= 0;$i<Count($array)-1;$i++) {//$i is the number of elements that have been ordered for($j= 0;$j<Count($array)-1-$i;$j++) {//$j is the number of elements that need to be sorted, minus $i by total length if($array[$j] >$array[$j+ 1]) {//Sort by Ascending $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 conditions set, swap position, form a new array = 1 5 4 7 J + +
if ($arr [1] > $arr [2]) = 5 > 4 condition established, swap position, form new array = 1 4 5 7
if ($arr [2] > $arr [3] = 5 > 7 condition not set, array remains unchanged, 1 4 5 7 J + + J=3 exit inner Loop, i++
http://www.bkjia.com/PHPjc/934724.html www.bkjia.com true http://www.bkjia.com/PHPjc/934724.html techarticle bubble sort-php, bubbling-php PHP implementation Code First: function Bubble_sort ($array) {for ($i = 0; $i count ($array)-1; $i + +) {//$i as already Number of ordered elements ...