Home  >  Article  >  Backend Development  >  Detailed Example of PHP Bubble Sort Algorithm_PHP Tutorial

Detailed Example of PHP Bubble Sort Algorithm_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:291813browse

When we were in school, our teacher told us that there are many algorithms for sorting program data. In fact, the bubble algorithm is a commonly used sorting algorithm. Now I will use PHP to implement bubble sort, and record it below.

Example 1

The code is as follows Copy code
 代码如下 复制代码

/**
 * 冒泡排序 (一维数组)
 * 两两比较待排序数据元素的大小,发现两个数据元素的次序相反时即进行交换,直到没有反序的数据元素为止
 * 设想被排序的数组R[1..N] 垂直竖立,将每个数据元素看作有重量的气泡,从下往上扫描数组,凡扫描违反原则的轻气泡,就使其向上"漂浮".如此反复进行.
 * 直到最后任何两个气都是轻者在上,重者在下为止.
 */
function bubble_sort($arr) {
        $count = count($arr);
        if($count = 0 || is_array($arr)) {
                echo 'NOT NULL ARRAY';
                return false;
        }
        for($i = 0; $i < $count; $i++) {
$flag = 1;
for($j = $count - 1; $j > $i; $j--) {
                        if($arr[$j] < $arr[$j - 1]) {
$tmp = $arr[$j]; //将数组的元素调换
$arr[$j] = $array[$j - 1];
$arr[$j - 1] = $tmp;
$flag = 0;
}
if (1 == $flag)
{
echo $i,'完成排序';
break;
}
}
}
return $arr;
}

/**
* Bubble sort (one-dimensional array)
* Compare the sizes of the data elements to be sorted pairwise. If it is found that the order of the two data elements is reversed, exchange them until there are no reversed data elements
* Imagine that the sorted array R[1..N] is erected vertically, and each data element is regarded as a weighted bubble. The array is scanned from bottom to top. Any light bubbles that violate the principle are scanned and made to "float" upward. .Do this over and over again.
* Until the end of any two qi, the lighter one is at the top and the heavier one is at the bottom.
​*/
function bubble_sort($arr) {
          $count = count($arr);
If($count = 0 || is_array($arr)) {
echo 'NOT NULL ARRAY';
                    return false;
         }
for($i = 0; $i < $count; $i++) {
                    $flag = 1;
for($j = $count - 1; $j > $i; $j--) {
If($arr[$j] < $arr[$j - 1]) {
                                                                                                                                                                                                                $arr[$j] = $array[$j - 1];
                                                             $flag = 0;
                            }
                           if (1 == $flag)
                                                                     echo $i,'Complete sorting';
                     break;
                   }
                   }
         }
         return $arr;
}<🎜>

Example 2

The code is as follows
代码如下 复制代码

//效率排序
//冒泡排 < 选择排序法 < 插入排序法
$arr = array(-1,10,99,89,77,101,3,4,5,22,11,56,32);
//冒泡排序法
$flag = false;
function bubbleSort($myarr)
{
$_temp = 0;
for ($i=0;$i {
for ($j=0;$j {
//如果前面的数比后面的数大,则两个数交换
if ($myarr[$j] > $myarr[$j+1])   
            {   
                $_temp = $myarr[$j];   
                $myarr[$j] = $myarr[$j+1];   
                $myarr[$j+1] = $_temp;   
                $flag = true;   
            }   
        }   
        //flage标志位,提升效率用   
        if (!$flag)   
        {   
            echo "已经是有序数组";   
            break;//已经是有序数组,直接退出   
        }   
        $flag = false;   
    }   
    return $myarr;   
}   
//原样输出   
function dump($string)   
{   
    echo "

";    <br>
    print_r($string);    <br>
    echo "
";   
}   
  dump(bubbleSort($arr));   
?>  

Copy code
//Efficiency sorting
//Bubble sort < Selection sort < Insertion sort
$arr = array(-1,10,99,89,77,101,3,4,5,22,11,56,32);
//Bubble sorting method
$flag = false;
function bubbleSort($myarr)
{ 
$_temp = 0;
for ($i=0;$i                              for ($j=0;$j                                                                                               // If the previous number is larger than the subsequent number, the two numbers exchange
If ($myarr[$j] > $myarr[$j+1])
                                                                                          $_temp = $myarr[$j];                                                                                         $myarr[$j] = $myarr[$j+1];                                                                                           $myarr[$j+1] = $_temp;                                $flag = true;
                                                                                                                                                                         //flage flag, used to improve efficiency
If (!$flag)
                                                                                                                                                                           echo "already an ordered array";      
                 break;//It is already an ordered array, exit directly                                                                                                                                                       $flag = false;                                         }  
Return $myarr;

//Output as is
function dump($string)
{ 
echo "
"; <br>
Print_r($string); <br>
echo "
";

dump(bubbleSort($arr));
?>

http://www.bkjia.com/PHPjc/633162.htmlwww.bkjia.comtrue
http: //www.bkjia.com/PHPjc/633162.html
TechArticle
When we were in school, our teacher told us that there are many algorithms for sorting program data. In fact, the bubble algorithm is one we commonly use. There is a sorting algorithm. Now I will use PHP to implement bubble sorting. The following notes...
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