Home > Article > Backend Development > PHP basic algorithm bubble sorting method
This article shares the code of the bubble sorting method in the basic algorithm of PHP. Friends in need can refer to it
<?php //冒泡排序法 function bubbleSort ($arr) { $len = count($arr); //该层循环控制 需要冒泡的轮数 for ($i=1; $i<$len; $i++) { //该层循环用来控制每轮 冒出一个数 需要比较的次数 for ($k=0; $k<$len-$i; $k++) { if($arr[$k] > $arr[$k+1]) { $tmp = $arr[$k+1]; // 声明一个临时变量 $arr[$k+1] = $arr[$k]; $arr[$k] = $tmp; } } } return $arr; } ?>
Related recommendations:
Four kinds of PHP Detailed explanation of basic algorithm
php basic algorithm_PHP tutorial
#The above is the detailed content of PHP basic algorithm bubble sorting method. For more information, please follow other related articles on the PHP Chinese website!