Home  >  Article  >  php教程  >  php数据结构 算法(PHP描述) 简单选择排序 simple selection sort

php数据结构 算法(PHP描述) 简单选择排序 simple selection sort

WBOY
WBOYOriginal
2016-06-06 20:38:361282browse

一次选定数组中的每一个数,记下当前位置并假设它是从当前位置开始后面数中的最小数min=i,从这个数的下一个数开始扫描直到最后一个数,并记录下最小数的位置min,扫描结束后如果min不等于i,说明假设错误,则交换min与i位置上数。

代码如下:
/**
* 简单选择排序 simple selection sort
*
* 原理: 一次选定数组中的每一个数,记下当前位置并假设它是从当前位置开始后面数中的最小数min=i,从这个数的下一个数开始扫描直到最后一个数,并记录下最小数的位置min,扫描结束后如果min不等于i,说明假设错误,则交换min与i位置上数。
*/
function sort_simple_selection($list)
{
$len = count($list);
if(empty($len)) return $list;
for($i = 0;$i {
$min = $i;
for($j = $i + 1; $j {
//if($list[$j] > $list[$min]) // 从大到小
if($list[$j] {
$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);
Statement:
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