Copy code The code is as follows:
/**
* Bubble sort bubble sort
*
* Principle: Loop multiple times for comparison, and move the maximum number to the top during each comparison. Each time through the loop, find the maximum value among the remaining variables, and then reduce the query range. After many loops in this way, the sorting of the array is completed
*/
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 large to small
{
$tmp = $list[$i];
$list[$i] = $list[$j];
$list[$j] = $tmp;
$flag = " change";
}
echo implode(',',$list).$flag."
";
}
echo "- --------------------------
";
}
return $list;
}
$list = array(4,3,2,1,5,7,3,7);
$list = sort_bubble($list);
http://www.bkjia.com/PHPjc/323859.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323859.htmlTechArticleCopy the code as follows: ?php /** * Bubble sort bubble sort * * Principle: loop multiple times Compare, moving the largest number to the top on each comparison. Each time it loops, find the remaining variables...