Home > php教程 > php手册 > php中实现快排与冒泡排序

php中实现快排与冒泡排序

WBOY
Release: 2016-06-13 10:57:43
Original
1338 people have browsed it

快排

function quicksort($str){
if(count($str) $key=$str[0];//取一个值,稍后用来比较;
$left_arr=array();
$right_arr=array();
for($i=1;$i if($str[$i] $left_arr[]=$str[$i];
else
$right_arr[]=$str[$i];
}
$left_arr=quicksort($left_arr);//进行递归;
$right_arr=quicksort($right_arr);
return array_merge($left_arr,array($key),$right_arr);//将左中右的值合并成一个数组;
}//以下是测试
$str=array(5,3,8,2,5,9,7,2,1,4,0);
print_r(quicksort($str));
?>
冒泡排序

function bubbingSort(array $array)
{
for($i=0, $len=count($array)-1; $i {
for($j=$len; $j>$i; --$j)
{
if($array[$j] {
$temp = $array[$j];
$array[$j] = $array[$j-1];
$array[$j-1] = $temp;
}
}
}
return $array;
}

print '

';<br>
print_r(bubbingSort(array(1,4,22,5,7,6,9)));<br>
print '
Copy after login
';


 

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template