简单算法 PHP部分字符串函数汇总

冒泡法:

 1 $arr =array(1,5,4,8,45,4,1,96,3);
 2     function maopao($arr){
 3         $len=count($arr);
 4         for($i=1;$i<$len;$i++){
 5             for($k=0;$k<$len-$i;$k++){
 6                 if($arr[$k]>$arr[$k+1]){
 7                     $temp =$arr[$k+1];
 8                     $arr[$k+1]=$arr[$k];
 9                     $arr[$k]=$temp;
10                 }
11             }
12         }
13         return $arr;
14     }
15     $a=maopao($arr);

选择排序:

 1 function selectSort($arr){
 2         $len=count($arr);
 3         for($i=0;$i<$len-1;$i++){
 4             $p=$i;
 5             for($j=$i+1;$j<$len;$j++){
 6                 if($arr[$p]>$arr[$j]){
 7                     $p=$j;
 8                 }
 9                 if($p != $i){
10                     $temp=$arr[$p];
11                     $arr[$p]=$arr[$i];
12                     $arr[$i]=$temp;
13                 }
14             }
15         }
16         return $arr;
17     }

 http://www.php100.com/html/dujia/2015/0210/8604.html

http://www.php100.com/html/dujia/2015/0212/8629.html