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