search
HomeBackend DevelopmentPHP ProblemHow to implement bubble sort algorithm in php

How to implement bubble sort algorithm in php

Sep 26, 2021 pm 04:21 PM
phpBubble Sort

php method to implement bubble sort algorithm: [for ($i=0; $i

How to implement bubble sort algorithm in php

The operating environment of this article: windows10 system , php 7, thinkpad t480 computer.

Before giving the specific implementation code, let’s briefly introduce bubble sorting.

Bubble sorting is a relatively simple sorting algorithm , it repeatedly visits the column of elements to be sorted, compares two adjacent elements at a time, and swaps them if their order (such as from large to small, first letter from A to Z) is wrong. The work of visiting elements It is repeated until no adjacent elements need to be exchanged, which means that the element has been sorted.

Then let's look at the principle of bubble sorting:

Compare adjacent elements. If the first is greater than the second, swap them both.

Do the same for each pair of adjacent elements, starting with the first pair and ending with the last pair. At this point, The last element should be the largest number.

Repeat the above steps for all elements except the last one.

Continue repeating the above steps for fewer and fewer elements each time, Until there is no pair of numbers to compare.

Finally, let’s take a look at the specific implementation code:

<?php
$arr = array(&#39;5&#39;,&#39;2&#39;,&#39;0&#39;,&#39;1&#39;,&#39;3&#39;,&#39;1&#39;,&#39;4&#39;);
 
function BubbleSort(array $arr)
{
 
	for ($i=0 ; $i <count($arr) ; $i++) { 
		//设置一个空变量
		$data = &#39;&#39;;
		for ($j=$i ; $j < count($arr)-1 ; $j++) { 
			if ($arr[$i] > $arr[$j+1]) {
 
				$data      = $arr[$i]; 
				$arr[$i]   = $arr[$j+1];
				$arr[$j+1] = $data; 
			}
		}
	}
 
	return $arr;
}
echo "<pre class="brush:php;toolbar:false">";
print_r(BubbleSort($arr));

Recommended learning: php training

The above is the detailed content of How to implement bubble sort algorithm in php. For more information, please follow other related articles on the PHP Chinese website!

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

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

Video Face Swap

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

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

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools