Home  >  Article  >  Backend Development  >  PHP implementation of selection sorting algorithm_PHP tutorial

PHP implementation of selection sorting algorithm_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:11:26843browse

[php]
function select_sort($arr){ 
    $len=count($arr); 
    for($i=0; $i<$len-1; $i++){ 
        $min=$i; 
        for ($j = $i+1; $j < $len; $j++) { 
            if($arr[$j]<$arr[$min]){ 
                $min=$j; 
            } 
        } 
        $tmp=$arr[$min]; 
        $arr[$min]=$arr[$i]; 
        $arr[$i]=$tmp; 
    } 
    return $arr; 

$arr=array(4,34,64,20,39);SelectSort($arr);print_r($arr); 
?> 

function select_sort($arr){
 $len=count($arr);
 for($i=0; $i<$len-1; $i++){
  $min=$i;
  for ($j = $i+1; $j < $len; $j++) {
   if($arr[$j]<$arr[$min]){
    $min=$j;
   }
  }
  $tmp=$arr[$min];
  $arr[$min]=$arr[$i];
  $arr[$i]=$tmp;
 }
 return $arr;
}
$arr=array(4,34,64,20,39);SelectSort($arr);print_r($arr);
?>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477335.htmlTechArticle[php] ?php function select_sort($arr){ $len=count($arr); for($i=0; $i$len-1; $i++){ $min=$i; for ($j = $i+1; $j $len; $j++) { if($arr[$j]$arr[$min]){ $min=$j; } } $tmp=$arr[$min];...
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