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

Detailed Example of PHP Bubble Sort Algorithm_PHP Tutorial

Jul 13, 2016 am 10:43 AM
php bubble exist School Example us sort data hour yes program algorithm teacher Detailed explanation

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles