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

WBOY
WBOYOriginal
2016-07-12 08:50:461166browse

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;>

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...
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