Home >Backend Development >PHP Tutorial >A simple example of a bubble sort function written in PHP, php bubble sort function_PHP tutorial
A question about algorithms I encountered a few days ago required the use of PHP language To sort an array, I wrote a function using bubble sorting method and share it with you.
<? //冒泡排序法 function bubble_sort($array) { $count = count($array); if($count <= 0) { return false; } for($i=0; $i<$count; $i++) { for($k=$count-1; $k>$i; $k--) { if($array[$k] < $array[$k-1]) { $tmp = $array[$k]; $array[$k] = $array[$k-1]; $array[$k-1] = $tmp; } } } return $array; } $arr = array(3, 5, 1, 4, 2); $s = bubble_sort($arr); print_r($s); ?>
The above simple example of a bubble sort function written in PHP is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia.