PHP算法:冒泡排序与快速排序

写一个排序算法,可以是冒泡排序或者快速排序,假设待排序对象是一个二维数组。(提示:不能使用系统已有函数,另外请仔细回忆以前学习过的基础知识)

//冒泡排序<br>
function bubble_sort($array)
{
&nbsp; &nbsp; <br>
	$count = count($array);
&nbsp; &nbsp; <br>
	if ($count <= 0) return false;

&nbsp; &nbsp; <br>
	for($i=0; $i<$count; $i++){
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br>
		for($j=$count-1; $j>$i; $j--){
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br>
			if ($array[$j] < $array[$j-1]){
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br>
				$tmp = $array[$j];
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br>
				$array[$j] = $array[$j-1];
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; <br>
				$array[$j-1] = $tmp;
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;<br>
			}
&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;<br>
		}
&nbsp; &nbsp; <br>
	}
&nbsp; &nbsp; <br>
	return $array;
<br>
}


你可能感兴趣的:(PHP算法:冒泡排序与快速排序)