Home > Backend Development > PHP Tutorial > A simple example of a bubble sort function written in PHP, php bubble sort function_PHP tutorial

A simple example of a bubble sort function written in PHP, php bubble sort function_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-12 08:50:46
Original
1187 people have browsed it

A simple example of a bubble sort function written in PHP, the php bubble sort function

A question about algorithms I encountered a few days ago required the use of PHP language To sort an array, I wrote a function using bubble sorting method and share it with you.

<&#63;
//冒泡排序法
function bubble_sort($array)
{
	$count = count($array);
	if($count <= 0)
	{
		return false;
	}
	for($i=0; $i<$count; $i++)
	{
		for($k=$count-1; $k>$i; $k--)
		{
			if($array[$k] < $array[$k-1])
			{
				$tmp = $array[$k];
				$array[$k] = $array[$k-1];
				$array[$k-1] = $tmp;
			}
		}
	}
	return $array;
}
$arr = array(3, 5, 1, 4, 2);
$s = bubble_sort($arr);
print_r($s);
&#63;>
Copy after login

The above simple example of a bubble sort function written in PHP is all the content shared by the editor. I hope it can give you a reference, and I hope you will support Bangkejia.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133069.htmlTechArticleA simple example of a bubble sort function written in PHP. I encountered the php bubble sort function a few days ago A question about algorithms requires the use of PHP language to sort an array. I...
Related labels:
source:php.cn
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template