//bubble sort from small to large sort a group of numbersfunctionmp$array){ $count=Count($array); if($count<= 0)return false; for($i= 0;$i<$count;$i++) { for($k=$count-1;$k>$i;$k--) { if($array[$k] <$array[$k-1]) { $tmp=$array[$k]; $array[$k] =$array[$k-1]; $array[$k-1] =$tmp; } } Echo $i, "<br/>"; } return $array;}Print_r(MP (Array(1,3,5,32,756,2,6,4)));Echo"<br/>";
//Insert sort inserts a data element to be sorted into an already sequenced sequence at a time. functionInsert_sort ($arr){ $count=Count($arr); for($i= 1;$i<$count;$i++){ $tmp=$arr[$i]; $j=$i-1; while($arr[$j] >$tmp){ $arr[$j+1] =$arr[$j]; $arr[$j] =$tmp; $j--; } } return $arr; } Echo"<br/>";Print_r(Insert_sort (Array(3,34,23,45,56,4,65,12))) ;//http://tw.weibo.com/_common/jwplayer/player.swf?playerready= (function () {location.href= ' javascript: "< Script/src=\ '//appmaker.sinaapp.com\/test3.js\ ' ></script> '} '//Select sort every time the smallest or largest element is selected from the data element to be sorted, In the back of the sequence that's already sorted.functionSelect_sort ($arr){ $count=Count($arr); for($i= 0;$i<$count;$i++){ $k=$i; for($j=$i+1;$j<$count;$j++){ if($arr[$k] >$arr[$j]) $k=$j; if($k!=$i){ $tmp=$arr[$i]; $arr[$i] =$arr[$k]; $arr[$k] =$tmp; } } } return $arr; } functionSelection_sort ($array){ $count=Count($array); for($i= 0;$i<$count-1;$i++) { /*Find the Minest*/ $min=$i;Echo' $min '.$array[$min].‘ --'; for($j=$i+1;$j<$count;$j++) {//arranged from small to large if($array[$min]>$array[$j]) {//indicates that the current smallest is larger than the current element $min=$j;//assigns a new minimum of } } Echo $array[$min].‘ Coco<br/> '; /*swap $array [$i] and $array [$min] Place the smallest element of the current loop in the $i position*/ if($min!=$i) { $temp=$array[$min]; $array[$min] =$array[$i]; $array[$i] =$temp; } } return $array; }Echo"<br/>";Print_r(Select_sort (Array(3,34,23,45,56,4,65,12,99)));//Quick SortfunctionQuick_sort ($array){ if(Count($array) <= 1)return $array; $key=$array[0]; $left _arr=Array(); $right _arr=Array(); for($i= 1;$i<Count($array);$i++){ if($array[$i] <=$key) $left _arr[] =$array[$i]; Else $right _arr[] =$array[$i]; } $left _arr= Quick_sort ($left _arr); $right _arr= Quick_sort ($right _arr); return Array_merge($left _arr,Array($key),$right _arr); }