Home  >  Article  >  Backend Development  >  PHP basic algorithm bubble sorting method

PHP basic algorithm bubble sorting method

不言
不言Original
2018-03-30 15:49:451555browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn