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

PHP快速排序问题

WBOY
Release: 2016-06-23 13:31:26
Original
949 people have browsed it

$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));
Copy after login

各位朋友好,小弟用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);}
Copy after login
Array(    [0] => 1    [1] => 2    [2] => 3    [3] => 4    [4] => 9)
Copy after login
自己看看区别在哪里

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template