Home > php教程 > php手册 > body text

用PHP写的一个冒泡排序法的函数简单实例,php冒泡排序函数

WBOY
Release: 2016-06-13 08:38:17
Original
1355 people have browsed it

用PHP写的一个冒泡排序法的函数简单实例,php冒泡排序函数

前几天遇到的一道关于算法的考题,要求用PHP语言实现对一个数组进行排序,我写了一个采用冒泡排序法的函数,和大家分享一下。

<&#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

以上这篇用PHP写的一个冒泡排序法的函数简单实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持帮客之家。

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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!