1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <?php function quick_sort($arr){ $length = count($arr); if($length <= 1) { return $arr; } $base_num = $arr[0]; $left_array = array(); $right_array = array(); for($i=1; $i<$length; $i++) { if($base_num > $arr[$i]) { $left_array[] = $arr[$i]; } else { $right_array[] = $arr[$i]; } } $left_array = quick_sort($left_array); $right_array = quick_sort($right_array); return array_merge($left_array, array($base_num), $right_array);; } $arr=array(5,1,0,3,9,10,59,41,78,56,45,47,12,15,45,11); $rs=quick_sort($arr); print_r($rs);
Array ( [0] => 0 [1] => 1 [2] => 3 [3] => 5 [4] => 9 [5] => 10 [6] => 11 [7] => 12 [8] => 15 [9] => 41 [10] => 45 [11] => 45 [12] => 47 [13] => 56 [14] => 59 [15] => 78 ) ?>
|