Copy code The code is as follows:
/**
* Bubble sort bubble sort
*
* Principle: Loop multiple times for comparison, and move the largest 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 like this, 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 big 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);
The above introduces cancelbubble PHP data structure algorithm description bubble sort bubble sort, including cancelbubble content, I hope it will be helpful to friends who are interested in PHP tutorials.