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 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));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 2341Summary 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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)