简单的快排算法及冒泡排序算法
<?php function quictSort($arr){ $count = count($arr); if($count<2){ return $arr; } $firstValue = array_shift($arr); $leftArr = $rightArr = []; foreach($arr as $k=>$v){ if($v<$firstValue){ $leftArr[] = $v; }else{ $rightArr[] = $v; } } return array_merge(quictSort($leftArr),array($firstValue),quictSort($rightArr)); } print_r(quictSort([78,56,43,24,13]));
<?php function maoPao($arr){ $count = count($arr); for($i=1;$i<$count;$i++){ for($j=0;$j<$count-$i;$j++){ if($arr[$j] > $arr[$j+1]){ $temp = $arr[$j+1]; $arr[$j+1] = $arr[$j]; $arr[$j] = $temp; } } } return $arr; } echo '<pre>'; print_r(maoPao([12,34,10,8,9,5]));