Home >php教程 >php手册 >php选择排序

php选择排序

WBOY
WBOYOriginal
2016-06-13 10:52:101106browse

[php]
 //选择排序 
 //从小到大排序 
  
  
//date_default_timezone_set('Aisa/Shanghai');  
 $select=array(); 
 for($i=0;$i  {  
     $select[$i]=rand(0,3000); 
 } 
function selectsort(&$arr) 
{    
  $temp=0; 
  for($i=0;$i     { 
      $minval=$arr[$i];  //每一次认为第i个数是最小值 
      $minindex=$i; 
      for($j=$i+1;$j           { 
          //说明目前的值并不是最小值 
          if($minval>$arr[$j]) 
              { 
               $minval=$arr[$j]; 
               $minindex=$j; 
              } 
          } 
          //内层for循环结束后再进行交换  这正是选择排序叫冒泡排序优越的地方 
          $temp=$arr[$i]; 
          $arr[$i]=$arr[$minindex]; 
          $arr[$minindex]=$temp; 
    } 
  

    selectsort($select); 
    print_r($select); 
    //date_default_timezone_set('Aisa/Shanghai'); 
?> 
[php]

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