How to implement bubble sorting using the php function: 1. Create a PHP sample file; 2. Define an array; 3. Implement bubble sorting through the "function bubble_sort($array){...}" method That’s it.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
How to use php function to implement bubble sort?
A simple example of a bubble sort function written in PHP
A few days ago I encountered a test question about an algorithm, which 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); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to implement bubble sort using php function. For more information, please follow other related articles on the PHP Chinese website!