Home > Backend Development > PHP Tutorial > PHP data structure algorithm description bubble sort bubble sort_PHP tutorial

PHP data structure algorithm description bubble sort bubble sort_PHP tutorial

WBOY
Release: 2016-07-21 15:26:57
Original
913 people have browsed it

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);

www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template