Copy CodeThe code is as follows:
/**
* Bubble Sort Bubble sort
*
* Principle: Multiple cycles are compared, the maximum number is moved to the top at each comparison. Each time you loop, find the maximum value in the remaining variable and then reduce the query range. So after many loops, the sort 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.html www.bkjia.com true http://www.bkjia.com/PHPjc/323859.html techarticle Copy the code code as follows:? PHP/** * Bubble Sorting bubble Sort * * principle: Multiple loops are compared, and the maximum number is moved to the top of each comparison. Each cycle, find out the remaining changes ...