Home Daily Programming PHP Knowledge PHP bubble sort algorithm (1)

PHP bubble sort algorithm (1)

Feb 26, 2019 pm 02:37 PM



##In the previous article, we introduced to you the PHP algorithm series "

PHP randomly picks one algorithm". Today we will continue to explain to you the relevant knowledge points of the common PHP algorithm series, namely PHP Bubble Sorting Algorithm.

PHP bubble sort algorithm (1)

Bubble sorting, I believe is no stranger to programmers. To put it simply, the bubble sort algorithm compares two adjacent numbers in sequence, and then sorts them according to size until the last two digits.

The reason why it is called the "bubble sort" algorithm is because larger elements will slowly "float" to the top of the array through exchange (arranged in ascending or descending order), just like the bubbles of carbon dioxide in carbonated drinks. It will eventually float to the top.


Below we will introduce to you the implementation of

PHP bubble sorting algorithm based on specific code examples. (The following is the ascending order, that is, from small to large)

The code example is as follows:

<?php
function maopao($arr){
    $len = count($arr);
    for($k=0;$k<=$len;$k++)
    {
        for($j=$len-1;$j>$k;$j--){
            if($arr[$j]<$arr[$j-1]){
                $temp = $arr[$j];
                $arr[$j] = $arr[$j-1];
                $arr[$j-1] = $temp;
            }
        }
    }
    return $arr;
}
$arr = [2,6,2,8,2,34,5,9,2341,23];

var_dump(maopao($arr));
Copy after login

Here we define a maopao method. First, we count the numbers through the count function Specifies the total length of the array. Then use a double for loop statement to loop through the array elements and compare them one by one. Then the outer for loop is used to control the loop rounds; the inner for loop is to control the number of comparisons in each round, and select the largest one after each round of comparison. The value is placed last.

Note: Here we store the value of $j through the temporary variable $temp medium, so as to compare two adjacent elements in a loop and put the larger value at the end.

Output:

array (size=10)
  0 => int 2
  1 => int 2
  2 => int 2
  3 => int 5
  4 => int 6
  5 => int 8
  6 => int 9
  7 => int 23
  8 => int 34
  9 => int 2341
Copy after login

Summary of the algorithm principle of bubble sort: For a set of data, compare the size of adjacent data, put the data with small values ​​in front and the data with large values ​​in the back.

This article is an introduction to the PHP bubble sort algorithm. In the next article "

PHP Bubble Sort Algorithm (2)", we will use XdebugDebug the implementation process of running the bubble sort algorithm more intuitively for everyone.



The above is the detailed content of PHP bubble sort algorithm (1). For more information, please follow other related articles on the PHP Chinese website!

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 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)