Copy Code code as follows:
<?php
/**
* Bubble Sort Bubble sort
*
* Principle: Multiple loops to compare, each time the maximum number of comparisons to move to the top. Each time you loop, find the maximum value in the remaining variable, and then reduce the scope of the query. This completes the ordering of this array after several loops.
*/
function sort_bubble ($list)
{
$len = count ($list);
if (empty ($len)) return $list;
for ($i = 0; $i < $len; $i + +)
{
for ($j = $i + 1; $j < $len; $j + +)
{
$flag = ';
if ($list [$i] > $list [$j])//from small to large
if ($list [$i] < $list [$j])//from Big to small
{
$tmp = $list [$i];
$list [$i] = $list [$j];
$list [$j] = $tmp;
$flag = "Change";
}
echo implode (', ', $list). $flag. " <br/> ";
}
echo "-------------------------<br/>";
}
return $list;
}
$list = Array (4,3,2,1,5,7,3,7);
$list = sort_bubble ($list);