Home >Backend Development >PHP Tutorial >PHP快速排序问题

PHP快速排序问题

WBOY
WBOYOriginal
2016-06-23 13:31:26952browse

$arr=array(4,1,9,2,3);function quick($arr){	if ( count($arr)<=1) {		return $arr;	}	for ($i=1; $i < count($arr); $i++) { 		if ($arr[0]<$arr[$i]) {			$right[]=$arr[$i];		}else{			$left[]=$arr[$i];		}	}	$right1=quick($right);	$left1=quick($left);	return array_merge($left1,array($arr[0]),$right1);}var_dump(quick($arr));

各位朋友好,小弟用PHP实现一个快速排序的问题。最后得出的的结果始终是NULL,百思不得其解。请开导,谢谢!


回复讨论(解决方案)

$arr=array(4,1,9,2,3);print_r(quick($arr));function quick($arr){    if ( count($arr)<=1) {         return $arr;    }    $left = array();    $righe = array();    for ($i=1; $i < count($arr); $i++) {         if ($arr[0]<$arr[$i]) {            $right[]=$arr[$i];        }else{            $left[]=$arr[$i];        }    }    $right1=quick($right);    $left1=quick($left);    return array_merge($left1,array($arr[0]),$right1);}
Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 9)
自己看看区别在哪里

如果 $left 和 $right 没有初值的话
你想想会是什么结果

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn