PHP basic algorithm bubble sorting method

不言
Release: 2023-03-22 19:58:01
Original
1537 people have browsed it

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;
}
?>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!