PHP BubbleSort

 1 /**
 2 * BubbleSort part 1
 3 * 
 4 * @param mixed $str
 5 */
 6 function BubbleSort($str) 
 7 { 
 8 $n=count($str);
 9 for ($i=0;$i<$n;$i++) 
10 {     
11     for ($j=$n-2;$j>=$i;$j--) 
12     { 
13             if($str[$j+1]<$str[$j]) 
14             {    
15                 $tmp = $str[$j+1]; 
16                 $str[$j+1]=$str[$j]; 
17                 $str[$j]=$tmp; 
18             } 
19 
20     } 
21 
22     
23 } 
24 $str=implode(",",$str);
25 return $str; 
26 } 
 1 /**
 2 * BubbleSort part 2
 3 * 
 4 * @var mixed
 5 */
 6 $source=array(3,6,1,5,9,0,4,6,11);
 7 define("array_length",count($source));
 8 for ($i=0;$i<array_length;$i++){
 9     for ($j=$i;$j<array_length;$j++){
10         if ($source[$i]>$source[$j]){
11             $temp=$source[$i];
12             $source[$i]=$source[$j];
13             $source[$j]=$temp;
14         }
15     }
16 }
17 print_r($source);  
posted on 2012-05-22 22:02  那瞬间  阅读(170)  评论(0)    收藏  举报