The problem is this:
Why does this print $left have array empty, array 0, and int 6 appear? Logically, recursive calls to array 0 + int 1 after the array is executed directly after the return of the 71 line $arr Ah, how can there be two arrays?
The source code is as follows:
$arr = array(6,3,8,6,4,2,9,5,1);function quick_sort($arr){ if(!is_array($arr)) return false; $length = count($arr); if($length <= 1) return $arr; $left = $right = array(); for($i = 1;$i < $length;$i++){ if($arr[$i] < $arr[0]){ $left[] = $arr[$i]; }else{ $right[] = $arr[$i]; } } var_dump($left); $left = quick_sort($left); $right = quick_sort($right); return array_merge($left,array($arr[0]),$right);}print_r(quick_sort($arr));
Reply content:
The problem is this:
Why does this print $left have array empty, array 0, and int 6 appear? Logically, recursive calls to array 0 + int 1 after the array is executed directly after the return of the 71 line $arr Ah, how can there be two arrays?
The source code is as follows:
$arr = array(6,3,8,6,4,2,9,5,1);function quick_sort($arr){ if(!is_array($arr)) return false; $length = count($arr); if($length <= 1) return $arr; $left = $right = array(); for($i = 1;$i < $length;$i++){ if($arr[$i] < $arr[0]){ $left[] = $arr[$i]; }else{ $right[] = $arr[$i]; } } var_dump($left); $left = quick_sort($left); $right = quick_sort($right); return array_merge($left,array($arr[0]),$right);}print_r(quick_sort($arr));
You dump is left, if the array is [1,3,3] is empty ah, [2,3,1] is a AH
$arr = Array (6,3,8,6,4,2,9,5,1);
$result = Array ();
function aaa ($arr, $result) {
$min = min($arr);array_push($result, $min);$key=array_search($min ,$arr);array_splice($arr,$key,1);if(count($arr)){ aaa($arr,$result);}else{ echo json_encode($result); exit;}
}
AAA ($arr, $result);
It feels easier with this idea.