Home >Backend Development >PHP Tutorial >PHP bubble sort array sorting method from small to large

PHP bubble sort array sorting method from small to large

小云云
小云云Original
2018-01-18 09:23:565483browse

本文主要和大家分享的是php冒泡排序之数组从小到大排序方法,希望能帮助到大家。
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;);

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!

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