Copy code code as follows:
<?php
/**
* Simple-select sort Easy-selection sorts
*
* principle: once Each number in the selected array, make a note of the current position and assume it is the smallest number in the number of min=i from the current position, starting at the next number of this number until the last number, and recording the minimum position min, if min is not equal to I at the end of the scan, the assumption is wrong, The number of min and I positions is exchanged.
*/
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])/from large to small
if ($list [$j] < $l ist[$min])//From small to large
{
$min = $j;
}
Echo implode (', ', $list). " #pos = ". ($min + 1). " Min= ". $list [$min]." <br/> ";
}
if ($min!= $i)
{
$temp = $list [$i];
$list [$i] = $list [$min];
$list [$min] = $temp;
}
Echo "-------------------------<br/>";
}
}
$list = Array (4,3,2,1,5,7,3,7);
$list = sort_simple_selection ($list);