PHP bubble sort array sorting method from small to large

小云云
Release: 2023-03-19 15:04:01
Original
5407 people have browsed it

本文主要和大家分享的是php冒泡排序之数组从小到大排序方法,希望能帮助到大家。
Copy after login
function compare($data,$order = 'asc'){
    if(empty($data))        return; $count = count($data);    for($i=0;$i<$count;$i++)
    {        for($j=$i+1;$j<$count;$j++)
        {            $tmp = $data[$i];   if($order == &#39;desc&#39;)
        {            if($data[$i] < $data[$j])
            {                $data[$i] = $data[$j];                $data[$j] = $tmp;
            }
        }        else
        {            if($data[$i] > $data[$j])
            {                $data[$i] = $data[$j];                $data[$j] = $tmp;
            }
        }
        }
    } return $data;
}$data = Array(3,2,1,6,8,10);$result =$this->compare($data,&#39;asc&#39;);
Copy after login

Related recommendations:

Detailed explanation of bubble sorting method

php bubble sorting method

A simple example of a bubble sort function written in PHP, php bubble sort function

The above is the detailed content of PHP bubble sort array sorting method from small to large. 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!