冒泡排序
两两相邻的数进行比较,如果反序就交换,否则不交换
时间负责度:O(n^2)
空间负责度:O(1)<?php $a = array(3,8,1,4,11,7); $len = count($a); for ($i=1; $i<$len; $i++) { for($j=$len-1; $j>=$i;$j--) { if ($a[$j] < $a[$j-1]){//如果改成从大到小的话,只要将if改成if ($a[$j] > $a[$j-1]) $temp = $a[$j]; $a[$j] = $a[$j-1]; $a[$j-1] = $temp; } } } print_r($a);
还不快抢沙发