<?php
class quick{
//快速排序
public function quick_sort($arr){
$base = $arr[0];
$left_array = array();
$right_array = array();
for($i = 1;$i<count($arr);$i++){
if($base>$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),$right_array);
}
}
$test_arr =array(23,12,3,88,16,45,2,66);
$sort = new quick();
$result = $sort->quick_sort($test_arr);
var_dump($result);
?>
打印显示$left_array = quick_sort($left_array);这里开始出错, Call to undefined function quick_sort()
6 回答
MYYA
TA贡献1868条经验 获得超4个赞
<?php
class quick {
//快速排序
public function quick_sort($arr){
if(count($arr) <= 1){
return $arr;
}
$base = $arr[0];
$left_array = $right_array = [];
for ($i = 1; $i < count($arr); $i++) {
if($base > $arr[$i]){
$left_array[] = $arr[$i];
} else {
$right_array[] = $arr[$i];
}
}
$left_array = $this->quick_sort($left_array);
$right_array = $this->quick_sort($right_array);
return array_merge($left_array, [$base], $right_array);
}
}
$test_arr = [23,12,3,88,16,45,2,66];
$sort = new quick();
$result = $sort->quick_sort($test_arr);
var_dump($result);
帮你修正了一下
慕田峪9158850
TA贡献1794条经验 获得超7个赞
$left_array = quick_sort($left_array);
$right_array = quick_sort($right_array);
改为
$left_array = $this->quick_sort($left_array);
$right_array = $this->quick_sort($right_array);
- 6 回答
- 0 关注
- 888 浏览
添加回答
举报
0/150
提交
取消