Home >Backend Development >PHP Tutorial >PHP bubble sort array sorting method from small to large
本文主要和大家分享的是php冒泡排序之数组从小到大排序方法,希望能帮助到大家。
function compare($data,$order = 'asc'){ if(empty($data)) return; $count = count($data); for($i=0;$i<$count;$i++) { for($j=$i+1;$j<$count;$j++) { $tmp = $data[$i]; if($order == 'desc') { if($data[$i] < $data[$j]) { $data[$i] = $data[$j]; $data[$j] = $tmp; } } else { if($data[$i] > $data[$j]) { $data[$i] = $data[$j]; $data[$j] = $tmp; } } } } return $data; }$data = Array(3,2,1,6,8,10);$result =$this->compare($data,'asc');
Related recommendations:
Detailed explanation of bubble sorting method
A simple example of a bubble sort function written in PHP, php bubble sort function
The above is the detailed content of PHP bubble sort array sorting method from small to large. For more information, please follow other related articles on the PHP Chinese website!