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

cancelbubble PHP data structure algorithm description bubble sort bubble sort

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-29 08:46:00
Original
1081 people have browsed it

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.

Related labels:
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