Home  >  Article  >  php教程  >  简单的冒泡排序

简单的冒泡排序

PHP中文网
PHP中文网Original
2016-05-25 16:59:301335browse

php代码

<?php
	/**
	| 简单的描述一下冒泡排序方法
	| 将数组中的数字从小到大排列
	**/
	
class buff_order
{
	public function to_order($array)
	{
		$k = 0;


		for ($i = 0; $i < count($array); $i++)
		{
			$max = $i+1;

			if (isset($array[$max]))
			{
				if($array[$i] > $array[$max])
				{
					$tmp = $array[$i];
					$array[$i] = $array[$max];
					$array[$max] = $tmp;
					$k++;
				}
			}
			
			if($k > 0)
			{
				$array = $this->to_order($array);
			}
		}
		
		return $array;
	}
}
$array = array(9,8,6,5,3,2,4,1,7);
$obj = new buff_order;
print_r($obj->to_order($array));

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