Home >php教程 >php手册 >选择排序算法的PHP实现

选择排序算法的PHP实现

WBOY
WBOYOriginal
2016-06-13 10:57:241551browse

[php]
function select_sort($arr){ 
    $len=count($arr); 
    for($i=0; $i         $min=$i; 
        for ($j = $i+1; $j             if($arr[$j]                 $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   $min=$i;
  for ($j = $i+1; $j    if($arr[$j]     $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);
?>

 

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