Simple selection sorting algorithm: through n-i comparisons between keywords, select the record with the smallest keyword from n-i+1 records, and combine it with the i-th (1<=i<=n) record Exchange
class Sort{
/**
* Simple selection sorting
*
* @param unknown_type $arr
*/
public function selectSort(&$arr) {
$len=count($arr);
for ($i=0;$i<$len;$i++) {
$min=$i;
for ($j=$i+1;$j<=$len-1;$j++) {
If ($arr[$min]>$arr[$j]) {//If a value smaller than $arr[$min] is found, assign the subscript to $min
$min=$j;
}
}
If ($min!=$i){//If $min is not equal to $i, it means the minimum value has been found, then exchange
$this->swap($arr[$i],$arr[$min]);
}
}
}
/**
* Swap the two values of $a and $b
*/
public function swap(&$a,&$b) {
$temp=$a;
$a=$b;
$b=$temp;
}
}
$arr=array(4,6,1,2,9,8,7,3,5);
$test=new Sort();
$test->selectSort($arr);//Simple selection sort
// var_dump($arr);
?>
Features of simple selection sorting: the number of mobile data exchanges is quite small, thus saving corresponding time
Time complexity analysis of simple selection sort:
Regardless of the best or worst case, the number of comparisons is the same. The i-th sorting requires n-i keyword comparisons, which requires n(n-1)/2 comparisons. So the final time complexity is O(n^2)
Although it is O(n^2) like bubble sort, the performance of selection sort is still slightly better than bubble sort.
http://www.bkjia.com/PHPjc/947915.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/947915.htmlTechArticlePHP simple selection sorting algorithm example, php algorithm example simple selection sorting algorithm: through n-i comparisons between keywords , select the record with the smallest keyword from the n-i+1 records, and combine it with the...