Foreword
Bubble sorting roughly means comparing two adjacent numbers in sequence, and then sorting according to size until the last two digits. Since in the sorting process, small numbers are always placed forward and large numbers are placed backward, which is equivalent to bubbles rising, so it is called bubble sorting. But in fact, in the actual process, you can also use it in reverse according to your own needs. The big trees are placed forward and the decimals are placed backward.
Actual combat
Go directly to the code:
<?php/** * 冒泡排序算法示例 */// 这里以一维数组做演示$demo_array = array(23,15,43,25,54,2,6,82,11,5,21,32,65);// 第一层for循环可以理解为从数组中键为0开始循环到最后一个for ($i=0;$i<count($demo_array);$i++) { // 第二层将从键为$i的地方循环到数组最后 for ($j=$i+1;$j $demo_array[$j]) { $tmp = $demo_array[$i]; // 这里的tmp是临时变量 $demo_array[$i] = $demo_array[$j]; // 第一次更换位置 $demo_array[$j] = $tmp; // 完成位置互换 } } }// 打印结果集echo ''; var_dump($demo_array);echo '';
Running result:
array(13) { [0]=> int(2) [1]=> int(5) [2]=> int(6) [3]=> int(11) [4]=> int(15) [5]=> int(21) [6]=> int(23) [7]=> int(25) [8]=> int(32) [9]=> int(43) [10]=> int(54) [11]=> int(65) [12]=> int(82) }
From the above results, we can see that the order of the key values in the array has been changed and the sorting is successful.
If the above algorithm is to sort the key values in the array from small to large according to the value size, then how to operate from large to small in reverse?
It’s very simple, just modify a comparison symbol, as follows:
<?php/** * 冒泡排序算法示例 */// 这里以一维数组做演示$demo_array = array(23,15,43,25,54,2,6,82,11,5,21,32,65);// 第一层for循环可以理解为从数组中键为0开始循环到最后一个for ($i=0;$i<count($demo_array);$i++) { // 第二层将从键为$i的地方循环到数组最后 for ($j=$i+1;$j<count($demo_array);$j++) { // 比较数组中相邻两个值的大小 if ($demo_array[$i] < $demo_array[$j]) { $tmp = $demo_array[$i]; // 这里的tmp是临时变量 $demo_array[$i] = $demo_array[$j]; // 第一次更换位置 $demo_array[$j] = $tmp; // 完成位置互换 } } }// 打印结果集echo ''; var_dump($demo_array);echo '';
Running result:
array(13) { [0]=> int(82) [1]=> int(65) [2]=> int(54) [3]=> int(43) [4]=> int(32) [5]=> int(25) [6]=> int(23) [7]=> int(21) [8]=> int(15) [9]=> int(11) [10]=> int(6) [11]=> int(5) [12]=> int(2) }
That’s it, change the order easily.
Extension
If you look closely at the above code, you will find that there is one place worth paying attention to, which is the place where swap variables are worth paying attention to. Yes, this is also the core point of bubbling. Once you master this technique, you can also use it in other places in the future.
Here we will talk about this a little bit.
Principle:
Now there are two variables A and B, and the requirement is to interchange their values.
When we see the title, we may first think of direct assignment, but if we assign directly, no matter who is assigned to whom first, one of them will definitely be overwritten. From this, we can come up with a third variable C, temporarily Store the value in A or B so that the required goal can be achieved.
$c = $a; // 暂存 $a = $b; // b给a $b = $c; // 暂存的a值再给b
Note: In fact, there is no need for a third variable. It is also possible to interchange the values of A and B variables. You can use substr(), str_replace() and other methods. Since we are introducing bubble sorting here, so But it’s not too much of an extension.
Summary
There are so many things about bubble sorting. To sum up, there are two main points:
Loop comparison
Exchange key values
If you can complete these two points, it is basically OK. Of course, there are many other algorithms for bubble sorting. Here is just one of them. Interested students can study it on their own.

Hot AI Tools

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

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

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.