PHP data structure algorithm (PHP description) simple selection sort simple selection sort_PHP tutorial

WBOY
Release: 2016-07-21 15:25:17
Original
981 people have browsed it

复制代码 代码如下:

/**
* simple selection sort simple selection sort
*
* Principle: Select each number in the array at a time, record the current position and assume that it is the smallest number among the following numbers starting from the current position min= i, start scanning from the next number to the last number, and record the position of the minimum number, min. After the scan, if min is not equal to i, it means that the assumption is wrong, then exchange the number at the position of min and i.
*/
function sort_simple_selection($list)
{
$len = count($list);
if(empty($len)) return $list;
for($i = 0;$i < $len; $i++)
{
$min = $i;
for($j = $i + 1; $j < $len; $j++)
{
//if($list[$j] > $list[$min]) // 从大到小
if($list[$j] < $list[$min]) // 从小到大
{
$min = $j;
}
echo implode(',',$list)."#pos=".($min + 1)." min=".$list[$min]."
";
}
if($min != $i)
{
$temp = $list[$i];
$list[$i] = $list[$min];
$list[$min] = $temp;
}
echo "-------------------------
";
}
}
$list = array(4,3,2,1,5,7,3,7);
$list = sort_simple_selection($list);

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/324189.htmlTechArticle复制代码 代码如下: ?php /** * 简单选择排序 simple selection sort * * 原理: 一次选定数组中的每一个数,记下当前位置并假设它是从当前位置开始...
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